Object assignment : Object Instance « Class Interface « C# / C Sharp






Object assignment

 


using System;
public class Foo
{
    public int i;
}
   
class RefTest1App
{
    static void Main(string[] args)
    {
        Foo test1 = new Foo();
        test1.i = 1;
   
        Foo test2 = new Foo();
        test2.i = 2;
   
        Console.WriteLine("BEFORE OBJECT ASSIGNMENT");
        Console.WriteLine("test1.i={0}", test1.i);
        Console.WriteLine("test2.i={0}", test2.i);
        Console.WriteLine();
   
        test1 = test2;
   
        Console.WriteLine("AFTER OBJECT ASSIGNMENT");
        Console.WriteLine("test1.i={0}", test1.i);
        Console.WriteLine("test2.i={0}", test2.i);
        Console.WriteLine();
   
        test1.i = 42;
   
        Console.WriteLine("AFTER CHANGE TO ONLY TEST1 MEMBER");
        Console.WriteLine("test1.i={0}", test1.i);
        Console.WriteLine("test2.i={0}", test2.i);
    }
}

 








Related examples in the same category

1.Object.ReferenceEquals Method
2.Calling an Instance Method on an Object
3.Calling a Class Method on a Class
4.Catch NullReferenceException Exception
5.Point object
6.Pass by ref for object parameters