Class object

As we discussed earlier in this chapter, all classes inherit directly or indirectly from the object class (System.Object in the FCL), so its seven methods are inherited by all other classes. Figure 10.19 summarizes object's methods.

Figure 10.19. object methods that are inherited directly or indirectly by all classes.

(This item is displayed on pages 501 - 502 in the print version)




This method compares two objects for equality and returns true if they are equal and false otherwise. The method takes any object as an argument. When objects of a particular class must be compared for equality, the class should override method Equals to compare the contents of the two objects. The method's implementation should meet the following requirements:

  • It should return false if the argument is null.
  • It should return true if an object is compared to itself, as in object1.Equals( object1 ).
  • It should return true only if both object1.Equals( object2 ) and object2.Equals( object1 ) would return true.
  • For three objects, if object1.Equals( object2 ) returns true and object2.Equals( object3 ) returns TRue, then object1.Equals( object3 ) should also return true.
  • A class that overrides the method Equals should also override the method GetHashCode to ensure that equal objects have identical hashcodes. The default Equals implementation determines only whether two references refer to the same object in memory.


This method cannot be explicitly declared or called. When a class contains a destructor, the compiler implicitly renames it to override the protected method Finalize, which is called only by the garbage collector before it reclaims an object's memory. The garbage collector is not guaranteed to reclaim an object, thus it is not guaranteed that an object's Finalize method will execute. When a derived class's Finalize method executes, it performs its task, then invokes the base class's Finalize method. Finalize's default implementation is a placeholder that simply invokes the base class's Finalize method.


A hashtable is a data structure that relates one object, called the key, to another object, called the value. We discuss Hashtable in Chapter 27, Collections. When initially inserting a value into a hashtable, the key's GetHashCode method is called. The hashcode value returned is used by the hashtable to determine the location at which to insert the corresponding value. The key's hashcode is also used by the hashtable to locate the key's corresponding value.


Every object knows its own type at execution time. Method GetType (used in Section 11.5) returns an object of class Type (namespace System) that contains information about the object's type, such as its class name (obtained from Type property FullName).


This protected method, which takes no arguments and returns an object reference, makes a copy of the object on which it is called. The implementation of this method performs a shallow copyinstance variable values in one object are copied into another object of the same type. For reference types, only the references are copied.


This static method takes two object arguments and returns TRue if two objects are the same instance or if they are null references. Otherwise, it returns false.


This method (introduced in Section 7.4) returns a string representation of an object. The default implementation of this method returns the namespace followed by a dot and the class name of the object's class.

We discuss several of object's methods throughout this book (as indicated in the table). You can learn more about object's methods in object's online documentation in the Framework Class Library Reference at:

All array types implicitly inherit from class Array in the System namespace, which in turn extends class object. As a result, like all other objects, an array inherits the members of class object. For more information about the class Array, please see Array's documentation in the FCL Reference, at:



    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look


    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2


    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures



    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types


    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: