.NET Type System, Part I - System.Object Class
(Page 3 of 6 )
The CTS has a base class, System.Object, that all other classes in the .NET Framework must derive directly or indirectly. At first you may think that it's not useful, but think about it: traditional programming languages has primitive types to store integers, floating point numbers and character strings besides the user defined data types in the form of classes. In these programming languages, the lack of a common base class for all other classes caused a lot of problems. In traditional languages built-in types have nothing in common, so you can write general purpose code (unlike C# and .NET Complaint Languages). In C# we can write code this way:
public void GetName(System.Object anyType)
{
Console.WriteLine(anyType.ToString());
}
This method accepts a paramter of type System.Object and calls the method ToString() on that Type. We can't do this in the same way in C or C++ and we will have to write wrapper classes and overload constructors for each type we want to support. The CTS comes to the rescue with its base class (System.Object); all objects derive from it, and this includes value types and reference types of built-in and use-defined types. The following are the public and protected methods of this class, which are implicitly inherited by any class.
Public methods
| Method | Description |
| ToString | This method returns a string to represent the object; it's like an answer for the question "What's your name?". It is virtual, which means that derived classes override it to supply an appropriate name for the object. For example, the Customer class may override this method to return the following pattern: CustomerFirstName + CustomerLastName; |
| Equals | This method behavior differs from value types and reference types. We will talk about this method in the last article (Objects manipulation and Operations). For now it is enough to say that it compares two objects and return true if they are equal and false if they are not. |
| GetType | This method returns a string that represents the Type of the object. |
| GetHashCode | Call this method on the object to retrieve a hash code that can be used in hash tables for faster performance. |
Protected methods
| Finalize | The CLR calls this method before garbage collection operates. This method is used for clean up and release operations of resources that has been used by your object. It's not guaranteed that the CLR will call this method, so you need to use another technique to release the resources. |
| MemberWiseClone | It's a shallow copy of the object. More on Copying objects later. |
Next: The Stack and the Heap >>
More .NET Articles
More By Michael Youssef