The NET Object Class

All the classes in C# end up deriving from a class built into the .NET Framework called object. If you write a class in C# and do not define a base class for it, the C# compiler silently derives it from object. Suppose that you write a C# class declaration without a class declaration, as follows:

class Point2D

This is equivalent to deriving your class from the .NET base class System.Object:

class Point2D : System.Object

The C# keyword object can be used as an alias for the System.Object identifier:

class Point2D : object

If you do derive from a base class, just remember that your base class either inherits from object or inherits from another base class that inherits from object. Eventually, your class inheritance hierarchy will include the .NET object class.

Thanks to the rules of inheritance in C#, the functionality of the .NET object class is available to all classes in C#. The .NET object class carries the following methods:

• public virtual bool Equals(object obj): Compares one object to another object and returns true if the objects are equal and false otherwise. This method is marked as virtual, which means that you can override it in your C# classes. You may want to override this method to compare the state of two objects of your class. If the objects have the same values for the fields, you can return true; you can return false if the values differ.

• public virtual int GetHashCode(): Calculates a hash code for the object. This method is marked as virtual, which means that you can override it in your C# classes. Collection classes in .NET may call this method to generate a hash code to aid in searching and sorting, and your classes can override this method to generate a hash code that makes sense for the class.

Note Hash code is a unique key for the specified object.

• public Type GetType(): Returns an object of a .NET class called Type that provides information about the current class. This method is not marked as virtual, which means that you cannot override it in your C# classes.

• public virtual string ToString(): Returns a string representation of your object. This method is marked as virtual, which means that you can override it in your C# classes. An object's ToString() method is called when .NET methods such as System.Console.WriteLine() need to convert a variable into a string. You can override this method to return a string more appropriate for representing your class's state. You may for example want to add the proper currency sign in front of the string representation of your Money class.

• protected virtual void Finalize(): May (or may not) becalled when the Common Language Runtime's garbage collector destroys the object. This method is marked as virtual, which means that you can override it in your C# classes. This method is also marked as protected, which means that it can only be called from within the class or a derived class, and cannot be called from outside the class hierarchy. The .NET object implementation of Finalize() does nothing, but you can implement it if you wish. You can also write a destructor for your class, which achieves the same effect (but be careful using this). In fact, the C# compiler translates your destructor code into an overridden Finalize() method. • protected object MemberwiseClone(): Creates a clone of the object, populates the clone with the same state as the current object, and returns the cloned object. This method is not marked as virtual, which means that you cannot override it in your C# classes. This method is also marked as protected, which means that it can only be called from within the class or a derived class, and cannot be called from outside the class hierarchy.

Structures in C# cannot have explicitly defined base classes, but they do implicitly inherit from the object base class. All the behavior of the object class is available to structures in C# as well as classes.

0 0

Post a comment