.NTOD System.Object

System.Object is the base type in the .NET class library.  All types in .NET inherit from the Object class and so inherit all of its members or what we normally call ‘offering low level services’ to all Types.  By understanding the basic methods offered by the Object you can access these methods in any other type in your code; That said, types will ‘override’ certain members of Object so that they operate slightly differently, I’ve pointed out which of these object members are virtual (can be overridden).  The Object() method is the constructor for this Class.

Important to note is that inheritance of this object in .NET is implied, so you don’t have to worry about writing ‘: Object’ when creating your own types.  The most commonly used members of the Object class are as follows:

myObject.Equals(object) //For reference types, this method checks reference equality (i.e. whether the object heap address is the same) for the object passed.  For value types, a bitwise equality check is carried out (which is just the binary bits of each object being the same).  Response = bool.  As this is a virtual method, it can be overridden in derived classes.

myObject.GetHashCode() //This method generates a number that relates to the value of the object.  So if you have a string and run this method you will generate a numeric value, but if you change the string, running this method will generate a different hash code.  This method is virtual so you can generate your own hashing algorhythm, but its usually best to use values of the object to create the code with.  You should note also that these hash codes do not create globally unique numbers by default unless you override the virtual method and use a GUID in your logic.

myObject.Finalize() // This method is called by the CLR when the object is garbage collected (provided the object has not been marked as ‘exempt’ from finalizing).  Any code you wish to run that cleans up the object code when the object is to be destroyed should be written into this method.   It is a protection method so is only accessible via the type or its derived types (so you can’t call it from another assembly).  By default, this method does nothing, so you have to create implementation code in your derived types yourself.  Is more commonly known as the objects ‘destructor’ as it performs the opposite actions to the objects default/custom constructor method.

myObject.GetType() //This method gets the ‘Type’ of the object instance.  It returns the System.Type object (which is used by the reflection process all of the time) which contains a bunch of members that describe the type, or more specifically provides the meta data about the type. This includes info like name, namespace, whether the type is an array etc.  The type name will be returned by default e.g. BaseClass myBaseClass = new BaseClass();  Console.WriteLine(“MyBaseClass is of type {0} : “, myBaseClass.GetType()) will return “MyBaseClass is of type BaseClass”.

myObject.ToString() //Returns a string object representation of the object value. Note that by default, the fully qualified name of the object is returned. Probably the most frequently used method of the object, well for me anyway.

Getting to know the Object class will help you out in a big way with all other types, e.g. you know you can return any object as a string object.  More tomorrow….


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s