Vous êtes sur la page 1sur 3

Understanding Object Data Type System.Object is a Common Type System to which any value can be assigned.

ed. Both Value Type and Reference Types can be assigned to variable of type object. Its a Reference Type. It can holds reference to any object on Heap memory.

Lets understand how Value Types and Object Type are related. Any value type including byte, short, int, long, float, double, decimal, char, bool, any enum and structure can be assigned to variable of type Object and this is called as Boxing. Boxing is the term used to describe the transformation from any Value Type to Object which is a Reference Type. The runtime creates a temporary reference -type box for the object on the heap. UnBoxing is the term used to describe the transformation from reference type (Object) to value type. We use the term cast here, as this has to be done explicitly. Boxing / Unboxing should be used only in situations where until runtime we dont know the type of data we are going to deal with. When a value is boxed to an object type, the object type variable cannot be used in any mathematical operations. When the value of object type variable cannot be assigned to variable on LHS, an Exception is thrown. Excessive usage of Object data type makes the language Loosely Typed and also because of frequent casting requirement while Boxing and Unboxing performance is also degraded. class Program { static void Main(string[] args) { object ob; int n, m; n = 10; ob = n; //Boxing - *1 m = (int)ob; //Unboxing - *2 string s = "Demo"; ob = s; // This is not boxing - *3 } } *1. Explanation: n is a local variable and is allocated memory on stack of Main and the value assigned to it is 10. ob is also a local variable and allocated memory on stack of Main This value of n, which is 10 is then copied on Heap and reference to it is assigned to the variable n. Because the value is on Heap and referenced by ob it is called Boxing *2. Explanation m is a local variable and is allocated memory on stack of Main. The value on the heap i.e 10, referenced by ob is copied to the location where variable m is stored. This is called as Unboxing.

*3. Explanation Here Demo is a string and strings being Reference Types are always allocated memory on Heap. The reference to the string is assigned to variable and same reference is assigned to ob. No copy of s is created. Same Value Demo is what ob referrers to. This is Not Boxing nor Unboxing.

Lets understand Object as Reference Type Every class in .NET is inherited from System.Object class. Because of this following are true about Object. o Variable of type object can reference to object of any class. o The functionality of Object class is available to all the objects in .NET. Methods in Object class 1. bool Equals(object obj) // Compares the current object reference with obj and returns true if both are referring to the same object otherwise returns false. 2. System.Type GetType() //returns the Type instance for the current objects class. 3. int GetHashCode() //return a unique number using which CLR identifies the current object. 4. string ToString() //return the class name of the current object. Note: ToString, Equals and GetHashCode are virtual and can be overridden in any class. Example: class Demo { public int N; public override string ToString() { return N.ToString(); } public override bool Equals(object obj) { Demo d = (Demo)obj; return this.N == d.N; //Instead of comparing reference we want to compare state of objects } } class Program { static void Main(string[] args) { Demo d1 = new Demo() { N = 10 }; Console.WriteLine(d1.ToString()); // Prints 10 Demo d2 = new Demo { N = 10 }; Console.WriteLine(d1.Equals(d2)); // Prints True Type t1 = d1.GetType();

Type t2 = d2.GetType(); Console.WriteLine(d1 == d2); //Prints True } } ToString: If ToString is not overridden in Demo class then default implementation as in object class returns class name and thus it prints Demo If ToString is overridden, then it prints 10 value of N as returned by ToString. Equals If Equals is not overridden then in Demo class the it prints False because both d1 and d2 are referencing to two different object and default implementation of Equals in object class returns true only if both the current and parameter variables refers to the same objects. If Equals is overridden as in the above example it prints True because we are not comparing the value of the members of object reference by current and parameter variables and they are same i.e. 10. GetHashCode Default implementation returns a unique number which is the identity of the object in CLR. If Equals is overridden it is recommended to override GetHashCode also and you can find a good explanation about why from http://stackoverflow.com/questions/371328/why-is-it-important-tooverride-gethashcode-when-equals-method-is-overriden-in-c. GetType: Its not a virtual method and thus cannot be overridden. Every Type when loaded has an instance of class System.Type is created. All objects of similar type have same Type instance.

Vous aimerez peut-être aussi