All types implicitly derived from System.Object
Public Methods
* Equals - Return true if two objects have the same value.
Reference types - objects - Return true if two references returned point to same object.
System.ValueType overrides the Equals method so that it returns true is the value of two object's fields match.
* GetHashcode - Return Hash code for this objects value. - implementation of the system.collections.Hashtable type requires that any two objects that are equal must have the same hash code value.
System.ValueType overrides the GetHashCode method so that it produces a hash code using an algorithm which takes into account the value of object's field.
* ToString - returns this.GetType().FullName.ToString() - int32, boolean etc override to return string of their value
* GetType - Non Virtual method - derived class cant override it - type safety.
Protected Methods
* MemberwiseClone - nonvirtual method - returns new instance of the type and sets the current object identical fields. first it allocated memory, initializes the objects overhead fields and then copies the source object's bytes to the new object.
* Finalize - automatically called just before relaclaming memory after garbage collecter is called. - types required cleanup is added in the override of this method.
No delete operator - No way explicitly free the memory allocated for an object
Initialize object overhead member - 2 members
* Object's pointer to the types method table
* SyncBlockIndex - multiple threads synchronize their access to the instance using the methods of the System.Threading.Monitor type
IS and AS
* IS returns boolean value after checking the compatibility of the object
* AS returns the object reference if found compatible else returns null
Object o1 = new Object();
Object o2 = new test1();
if (o1 is test1) //false
{
test1 t1 = (test1) o1;
Console.WriteLine("t1 success");
}
if (o2 is test1) //true
{
test1 t2 = (test1) o2;
Console.WriteLine("t2 success");
}
test1 t3= o1 as test1;
test1 t4 = o2 as test1;
if (t3 == null)
{Console.WriteLine("t3 is null");} //null
if (t4 == null)
{Console.WriteLine("t3 is null");} //not null
// x allocated to stack
Int32 x = 5;
// memory equal to size of x + method table pointer + SyncBlockIndex is allocated in heap and value copied then ref is returned
Object o = x;
//unboxing
Int16 y = (Int16)(Int32) o;
IL generated for the below is same
* System.Int32 a
* int a = 0
Primitive types - Any data types the compiler directly supports.
C# allows implicit casts if the conversion is "safe",that is no loss of data is possible such as converting int32 to an int64. But C# requires explicit casts if the conversin is potentially unsafe.For numeric types, "unsafe" means that you could lose precision or magnitude as a result of the conversion.
CHECKED AND UNCHECKED is used to check the overflow
System.Object -> System.ValueType -> System.Int32 or System.Boolean
System.Object -> System.ValueType -> System.Enum -> System.FontStyle or System.IO.FileAttributes
* All value types are either structures or enumerations inherit System.Object and thats why allocated on stack.
* CLR doesnt allow a value type to be used as a base type for any new types.
When any of the inherited methods of value type is called. It is first boxed and then method is accessed via table method pointer
ICloneable interface - The class should implement this interface if it wants instances of itself to be cloneable.
Public interface ICloneable{
Object Clone();
}
Friday, December 5, 2008
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment