Thursday, 1 March 2012

Article 5:-How Object is Created?

For Training with C# Click Here

Written By:-Isha Malhotra

How Object is Created?
Why classes are called Reference Type

As we know that class is a user defined data type.
For creating the object of any class we write the following code:-
Class_Name  Object_Name=new Class_Name();
Suppose the class name is calculator. Then we create the object like that
Calculator cal = new calculator();

This declaration done in two steps:-

ü  First it declares the variable cal of the class type calculator. This variable does not define any object. It is simply a reference variable.
                                  Calculator cal; //Reference variable of type class Calculater
ü  Second this declaration creates an actual physical copy of object and assign its(object) reference to cal which is a reference variable. It is done by using  the new operator.
The new operator dynamically allocates the memory for an object and return refrence to the cal (reference variable).
Cal=new calculator();
This is the reason in c# all class object allocates memory dynamically and Classes called as Reference Type. Now cal is linked with an object. 

No comments:

Post a Comment