6.4 Looking at System.Object


6.4 Looking at System.Object

Here's something that makes C# look more like Java than its official ancestor C++.

Like Java

  • Only single class inheritance is supported in C#. A C# class can implement multiple interfaces. [5]

    [5] C++ supports multiple class inheritance and does not support interfaces.

  • All Java classes are implicitly subclasses of java.lang.Object . In C#, all classes are implicitly subclasses of System.Object . [6] This implies that all C# classes must have one, and only one direct superclass, except for System.Object .

    [6] There are several advantages of having all your classes share a common ancestry. At the very least, you are assured that whatever object a method returns can be referenced by a variable of type System.Object . Also, there are several useful methods in System.Object that are inherited to every single class.

  • Like java.lang.Object , System.Object has several useful methods which are inherited by all C# classes, and can be overridden for more specific functionality in their respective contexts.

Table 6.3 gives a list of the important methods of System.Object (note that this class has three different methods for comparing object equality!).

Additional note

System.Object is represented by the alias object in C#. You can just use object instead of spelling out the full name System.Object in your codes (in very much the same way that you can use the C# alias string instead of System.String , or int instead of System.Int32 ).

Table 6.3. The methods of System.Object

Method signature

Comments

public virtual string ToString ()

Similar to java.lang.Object 's toString() . By default, in C#, this method returns the type of the object rather than a hash code (as in the case for Java [1] ). This method can be overridden in a subclass. [2]

public virtual int GetHashCode ()

Similar to java.lang.Object 's hashCode() . Returns the hash code for this object. An object's hash code will be useful for uniquely identifying an object in a collection (such as a dictionary).

public virtual bool Equals (object a)

These three methods are similar to java.lang.Object 's equals() . They are all used to compare two objects (see section 10.5).

Here are the differences:

  • The virtual Equals() method is to be overridden in subclasses for custom equality comparisons. In C#, only methods declared with the virtual keyword can be overridden in subclasses, hence this is the only Equals() method you can override.

  • The static Equals() method checks if the parameters are null . If both are null , it returns true . If only one of them is null , it returns false . If neither is null , it invokes the virtual Equals() method described above.

  • ReferenceEquals() is used to compare if two variables are referring to the same object instance. [3] Note that when two nulls are passed as parameters, ReferenceEquals returns true . When only one of the parameters is null , the method returns false . The following code fragment should clarify the use of ReferenceEquals() :

 MyClass c1 = new MyClass(); MyClass c2 = new MyClass(); MyClass c3 = c1; bool b1 =ReferenceEquals(null,null);//true bool b2 =ReferenceEquals(c1,c2); // false bool b3 =ReferenceEquals(c1,c3); // true bool b4 =ReferenceEquals(c1,null); //false 

public static bool Equals (object a, object b)

public static bool ReferenceEquals (object a, object b)

public Type GetType ()

Similar to java.lang.Object 's getClass() . GetType() returns an instance of System.Type which you can use for extracting more information about the object's type via the reflection API.

protected object MemberwiseClone ()

Similar to java.lang.Object 's clone() . This method returns a new object of the same type. Simple value variables (e.g. int , long ) of the returned object are copied over. If the object contains references to other 'embedded' objects, only the references are copied over to the returned object. No new embedded objects are created. [4]

protected virtual Finalize ()

Similar to java.lang.Object 's finalize() . Put clean-up code here.

[1] In Java's case, the toString() method defined in java.lang.Object returns a String with the type name, followed by an ' @ ' symbol, followed by the hash value for the object (something like 'Test@720eeb' where Test is this object's class type).

[2] Notice that the method has a virtual modifier. The virtual modifier will be covered later, but for now, all you need to know is that a virtual method is one which can be overridden in subclasses. A method declared without the virtual modifier cannot be overridden.

[3] The ReferenceEquals method is similar to using == operator for comparing object reference variables in Java.

[4] Both System.Object.MemberwiseClone() and java.lang.Object.clone() perform what is called ' shallow copying' instead of 'deep copying'.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net