Object Creation And Difference Between Object And Reference Variable In C#

Class and object

Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.
  1. Box b1;//Reference variable,can not create memory  
  2. b1 is called a reference variable (not object)  
  3. now b1 is null  
  4. Box b2 = new Box();// object b2 of class BOX is instantiated and create memory on Heap  
  5. now b2 points box object  
  6. Box b3 = new Box();  
  7. b3.length = 10;  
  8. b3.width = 20;  
  9.   
  10. b1 = b3; //an object is assigned to a reference variable  
now both b1 and b3 both points to same memory location..if any one updated then another one automatically updated.

Sample code
  1. namespace ConsoleApplicationdemo {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             Box b1; //Reference variable,not create memory  
  5.             Box b2 = new Box(); // object b2 of class BOX is instantiated and create memory on Heap  
  6.             Console.WriteLine("b2 object length:{0}", b2.length); //by default vale  
  7.             Console.WriteLine("b2 object width:{0}", b2.width); //by default vale  
  8.             Box b3 = new Box();  
  9.             b3.length = 10;  
  10.             b3.width = 20;  
  11.             Console.WriteLine("b3 object length:{0}", b3.length);  
  12.             Console.WriteLine("b3 object width:{0}", b3.width);  
  13.             b1 = b3; //an object is assigned to a reference variable  
  14.             //now reference variable works like an object  
  15.             //both b3 and b1 points to same memory location if any one updated then another one automatically updated  
  16.             Console.WriteLine("after assigning b3,the b1 object length:{0}", b1.length);  
  17.             Console.WriteLine("after assigning b3,the b1 object width:{0}", b1.width);  
  18.             ///////////////////////////modify b3  
  19.             b3.length = 45;  
  20.             b3.width = 55;  
  21.             Console.WriteLine("Modified b3 object length:{0}", b3.length);  
  22.             Console.WriteLine("Modified b3 object width:{0}", b3.width);  
  23.             Console.WriteLine("after modify b3,the b1 object length:{0}", b1.length);  
  24.             Console.WriteLine("after modify b3,the b1 object width:{0}", b1.width);  
  25.             ///////////////////////////modify b1  
  26.             b1.length = 70;  
  27.             b1.width = 80;  
  28.             Console.WriteLine("Modified b1 object length:{0}", b1.length);  
  29.             Console.WriteLine("Modified b1 object width:{0}", b1.width);  
  30.             Console.WriteLine("after modify b1,the b3 object length:{0}", b3.length);  
  31.             Console.WriteLine("after modify b1,the b3 object width:{0}", b3.width);  
  32.             Console.ReadKey();  
  33.         }  
  34.     }  
  35.     class Box {  
  36.         public int length;  
  37.         public int width;  
  38.         int height; // by default private ,any object of class cant access this data field  
  39.     }  
Output

b2 object length:0
b2 object width:0
b3 object length:10
b3 object width:20
after assigning b3,the b1 object length:10
after assigning b3,the b1 object width:20
Modified b3 object length:45
Modified b3 object width:55
after modify b3,the b1 object length:45
after modify b3,the b1 object width :55
Modified b1 object length:70"
Modified b1 object width:80
after modify b1,the b3 object length:70
after modify b1,the b3 object width:80