GetHashCode
A hash code is a number that represents your object. This number is not only used in hashing algorithms but it can help when comparing two objects (if their hash code is equal, the objects are equal). The problem here is that the hash code doesn’t necessarily give you a unique number. This can be seen when working with the TimeSpan or DateTime object since its GetHashCode implementation is based on the Ticks-property. Also, the value returned by this method is platform dependent. Meaning, the method will give you different values depending if you are running on 32-bit or 64-bit versions of the .NET Framework. So, when do we use this? Mostly when working with hash table collections. The GetHashCode() method is called if your object is used as a key in a collection.
Again, MSDN provides us with a list of rules that a decent implementation of the GetHashCode-method must obey to have a desired behavior:
· If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.
· The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again.
· For the best performance, a hash function must generate a random distribution for all input.
· Implementations of GetHashCode must not throw exceptions
· Derived classes that override GetHashCode must also override the Equals method to guarantee that two objects considered equal have the same hash code; otherwise, the Hashtable type might not work correctly
In our example, we can let GetHashCode return the ChassisNumber-value.
GetType
This returns an object of type Type, which describes the type of the calling object. This object of type Type gives access to the metadata linked to the class of the calling object. This is very interesting in the context of reflection. This method can’t be overridden which is normal, since it would enable you to change the return value of this method. Basically, allowing you to lie. This can cause many problems concerning type safety. Or can cause strange behavior in a situation where you, at runtime, look at an object using GetType() to see what type it is and extract information from the object. Imagine I could let my Car-class claim it was of type House. Trying to access let’s say the address-property would result in an error at runtime.
ToString
By default this returns the name of the type of the object on which the method is called. So what it returns is GetType().ToString(). The ToString()-implementations of the Type-object actually returns the full name of the object. This method is frequently overridden. The .NET basisc data types have this method overridden to return a string-representation of their value (Boolean, Char, Double, Decimal,…). When looking at the overridden ToString-methods of numbers (Int, Decimal, Double, Single) using a tool like .NET Reflector, notice that CultureInfo is taken into account in the form of NumberFormatInfo and implementing IFormattable. This will format the value of the object to the correct textual representation (commas, currency information)
Implementing this is as easy as for the other methods.
1: public override string ToString()
2: {
3: return "Chassis Number: " + this.ChassisNumber + " - Brand: " + this.Brand + " - Model: " + this.model;
4: }
From the main program, you can simply call the ToString() method on the car object:
Console.WriteLine(car1.ToString());
