Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   All Types Are Derived from System.Object

The runtime requires every type to ultimately be derived from the System.Object type. This means

that the following two type definitions are identical.

 

// Implicitly derived from Object   // Explicitly derived from Object
class Employee {   class Employee : System.Object {
...   ...
} }  

Because all types are ultimately derived from System.Object, you are guaranteed that every object of every type has a minimum set of methods. Specifically, the System.Object class offers the public instance methods listed in Table 4-1.

In addition, types that derive from System.Object have access to the protected methods listed in Table 4-2.


TABLE 4-1Public Methods of System.Object

 

Public Method Description
Equals Returns true if two objects have the same value. For more information about this method, see the “Object Equality and Identity” section in Chapter 5, “Primitive, Reference, and Value Types.”
GetHashCode Returns a hash code for this object’s value. A type should override this method if its objects are to be used as a key in a hash table collection, like Dictionary. The method should provide a good distribution for its objects. It is unfortunate that this method is defined in Object because most types are never used as keys in a hash table; this method should have been defined in an interface. For more information about this method, see the “Object Hash Codes” section in Chapter 5.
ToString The default implementation returns the full name of the type (this.GetType().FullName). However, it is common to override this method so that it returns a String object containing a representation of the object’s state. For example, the core types, such as Boolean and Int32, override this method to return a string representation of their values. It is also common to over- ride this method for debugging purposes; you can call it and get a string showing the values of the object’s fields. In fact, Microsoft Visual Studio’s debugger calls this function automatically to show you a string representation of an object. Note that ToString is expected to be aware of the CultureInfo associated with the calling thread. Chapter 14, “Chars, Strings, and Working with Text,” discusses ToString in greater detail.
GetType Returns an instance of a Type-derived object that identifies the type of the object used to call GetType. The returned Type object can be used with the reflection classes to obtain metadata information about the object’s type. Reflection is discussed in Chapter 23, “Assembly Loading and Reflection.” The GetType method is nonvirtual, which prevents a class from overriding this method and lying about its type, violating type safety.

 

TABLE 4-2Protected Methods of System.Object

 

Protected Method Description
MemberwiseClone This nonvirtual method creates a new instance of the type and sets the new object’s instance fields to be identical to the this object’s instance fields. A reference to the new instance is returned.
Finalize This virtual method is called when the garbage collector determines that the object is gar- bage and before the memory for the object is reclaimed. Types that require cleanup when collected should override this method. I’ll talk about this important method in much more detail in Chapter 21, “The Managed Heap and Garbage Collection.”

 



The CLR requires all objects to be created using the new operator. The following line shows how to create an Employee object.

 

Employee e = new Employee("ConstructorParam1");

 

Here’s what the new operator does:

 

1.It calculates the number of bytes required by all instance fields defined in the type and all of its base types up to and including System.Object (which defines no instance fields of its own). Every object on the heap requires some additional members—called the type object pointer and the sync block index—used by the CLR to manage the object. The bytes for these additional members are added to the size of the object.


2.It allocates memory for the object by allocating the number of bytes required for the specified

type from the managed heap; all of these bytes are then set to zero (0).

 

3.It initializes the object’s type object pointer and sync block index members.

4.The type’s instance constructor is called, passing it any arguments (the string "Constructor­ Param1" in the preceding example) specified in the call to new. Most compilers automatically emit code in a constructor to call a base class’s constructor. Each constructor is responsible for initializing the instance fields defined by the type whose constructor is being called. Eventu- ally, System.Object’s constructor is called, and this constructor method does nothing but return.

After new has performed all of these operations, it returns a reference (or pointer) to the newly created object. In the preceding code example, this reference is saved in the variable e, which is of type Employee.

By the way, the new operator has no complementary delete operator; that is, there is no way to explicitly free the memory allocated for an object. The CLR uses a garbage-collected environment (described in Chapter 21) that automatically detects when objects are no longer being used or ac- cessed and frees the object’s memory automatically.

 

 


Date: 2016-03-03; view: 649


<== previous page | next page ==>
Publisher Policy Control | Nbsp;   Casting Between Types
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)