Object

Object CF 1.0, ECMA 1.0, serializable

System (mscorlib.dll) class

This class is the root of all .NET types, including value types and reference types. Some CLR languages such as C# and VB.NET do not require a type to inherit from Object explicitly. If no base type is listed in a class declaration, it is assumed that the type is to inherit from Object . Therefore, all types derive from it implicitly and can use any of its methods .

Use the GetType( ) method to obtain a description of your object's internal metadata as a Type object. Use the ToString( ) method to get a String that represents your object. By default, this is the fully qualified type name of your object, but most classes override this method to provide something more useful, such as a string representation of the object's content. For example, System.Drawing.Point.ToString( ) might return (10 , 5) .

The MemberwiseClone( ) method returns a new object of the same type that is a member-by-member duplicate. This object is called a shallow copy because any subobjects are not copied . Instead, the references are duplicated , meaning that both the original and cloned type refer to the same subobjects. MemberwiseClone( ) is protected, so it can be called only from methods of your derived object. Usually, you will implement the ICloneable interface for your objects and call MemberwiseClone( ) from a customized ICloneable.Clone( ) method.

Use the Equals( ) method to test for reference equality. Derived value-type classes override this method to provide value equality (which returns true for identical content, even if it is stored in different objects at different memory addresses). Note that the equality operator ( = = in C#, = in VB.NET) does not call Equals( ) unless the equality operator is overloaded for the appropriate type (as it is with String , for example).

The ReferenceEquals( ) method, while perhaps seeming somewhat similar, compares object identity rather than object equality. That is, while Equals( ) might return true for two independent objects that contain the same state, ReferenceEquals( ) checks to see if the two references passed to it point to the same object. These two objects are identical, which is only the case when both references point to the same location in memory. Because the = = operator compares references by default, but could be changed later to compare state (or anything else), ReferenceEquals( ) is the only safe way to test references for identity.

The GetHashCode( ) method returns a hash code so the object can be used as a key in a System.Collections.Hashtable collection. By default, GetHashCode( ) returns a unique hash code for each object, which is sufficient for reference types but must be overridden by all value types so that equivalent types return identical hash codes.

 public class  Object  {  // Public Constructors  public  Object  ( );  // Public Static Methods  public static bool  Equals  (object   objA   , object   objB   );    public static bool  ReferenceEquals  (object   objA   , object   objB   );  // Public Instance Methods  public virtual bool  Equals  (object   obj   );    public virtual int  GetHashCode  ( );    public Type  GetType  ( );    public virtual string  ToString  ( );  // Protected Instance Methods  protected override void  Finalize  ( );    protected object  MemberwiseClone  ( ); } 

Subclasses

Multiple types

Returned By

Multiple types

Passed To

Multiple types



C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

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