Home > Articles > Programming > Windows Programming

Common Object Operations in .NET

📄 Contents

  1. Object Equality and Identity
  2. Object Hash Codes
  3. Object Cloning
In this sample chapter, Jeffrey Richter walks you through how to properly implement the operations that all objects must exhibit. Specifically, he introduces you to object equality, identity, hash codes, and cloning so that you can see how to treat these operations on your own objects.
This article is excerpted from Applied Microsoft .NET Framework Programming (Copyright 2002), by Jeffrey Richter. Reproduced by permission of Microsoft Press. All rights reserved.

Object Equality and Identity

The System.Object type offers a virtual method, named Equals, whose purpose is to return true if two objects have the same "value." The .NET Framework Class Library (FCL) includes many methods, such as System.Array's IndexOf method and System.Collections.ArrayList's Contains method, which internally call Equals. Because Equals is defined by Object, and because every type is ultimately derived from Object, every instance of every type offers the Equals method. For types that don't explicitly override Equals, the implementation provided by Object (or the nearest base class that overrides Equals) is inherited. The following code shows how System.Object's Equals method is essentially implemented:

class Object {
  public virtual Boolean Equals(Object obj) {

   // If both references point to the same 
   // object, they must be equal.
   if (this == obj) return(true);

   // Assume that the objects are not equal.
   return(false);
  }
  §
}

As you can see, this method takes the simplest approach possible: If the two references being compared point to the same object, true is returned; in any other case, false is returned. If you define your own types and you want to compare their fields for equality, Object's default implementation won't be sufficient for you; you must override Equals and provide your own implementation.

When you implement your own Equals method, you must ensure that it adheres to the four properties of equality:

  • Equals must be reflexive; that is, x.Equals(x) must return true.

  • Equals must be symmetric; that is, x.Equals(y) must return the same value as y.Equals(x).

  • Equals must be transitive; that is, if x.Equals(y) returns true and y.Equals(z) returns true, then x.Equals(z) must also return true.

  • Equals must be consistent. Provided that there are no changes in the two values being compared, Equals should consistently return true or false.

If your implementation of Equals fails to adhere to all these rules, your application will behave in strange and unpredictable ways.

Unfortunately, implementing your own version of Equals isn't as easy and straightforward as you might expect. You must do a number of operations correctly, and, depending on the type you're defining, the operations are slightly different. Fortunately, there are only three different ways to implement Equals. Let's look at each pattern individually.

Implementing Equals for a Reference Type Whose Base Classes Don't Override Object's Equals

The following code shows how to implement Equals for a type that directly inherits Object's Equals implementation:

// This is a reference type (because of 'class').
class MyRefType : BaseType {
  RefType refobj;  // This field is a reference type.
  ValType valobj;  // This field is a value type.

  public override Boolean Equals(Object obj) {
   // Because 'this' isn't null, if obj is null, 
   // then the objects can't be equal.
   if (obj == null) return false;

   // If the objects are of different types, they can't be equal.
   if (this.GetType() != obj.GetType()) return false;

   // Cast obj to this type to access fields. NOTE: This cast can't
   // fail because you know that objects are of the same type.
   MyRefType other = (MyRefType) obj;

   // To compare reference fields, do this:
   if (!Object.Equals(refobj, other.refobj)) return false;

   // To compare value fields, do this: 
   if (!valobj.Equals(other.valobj)) return false;

   return true;  // Objects are equal.
  }

  // Optional overloads of the == and != operators
  public static Boolean operator==(MyRefType o1, MyRefType o2) {
   if (o1 == null) return false;
   return o1.Equals(o2);
  }

  public static Boolean operator!=(MyRefType o1, MyRefType o2) {
   return !(o1 == o2);
  }
}

This version of Equals starts out by comparing obj against null. If the object being compared is not null, then the types of the two objects are compared. If the objects are of different types, then they can't be equal. If both objects are the same type, then you cast obj to MyRefType, which can't possibly throw an exception because you know that both objects are of the same type. Finally, the fields in both objects are compared, and true is returned if all fields are equal.

You must be very careful when comparing the individual fields. The preceding code shows two different ways to compare the fields based on what types of fields you're using.

  • Comparing reference type fields—To compare reference type fields, you should call Object's static Equals method. Object's static Equals method is just a little helper method that returns true if two reference objects are equal. Here's how Object's static Equals method is implemented internally:

    public static Boolean Equals(Object objA, Object objB) {
      // If objA and objB refer to the same object, return true.
      if (objA == objB) return true;
    
      // If objA or objB is null, they can't be equal, so return false.
      if ((objA == null) || (objB == null)) return false;
    
      // Ask objA if objB is equal to it, and return the result.
      return objA.Equals(objB);
    }

    You use this method to compare reference type fields because it's legal for them to have a value of null. Certainly, calling refobj.Equals(other.refobj) will throw a NullReferenceException if refobj is null. Object's static Equals helper method performs the proper checks against null for you.

  • Comparing value type fields—To compare value type fields, you should call the field type's Equals method to have it compare the two fields. You shouldn't call Object's static Equals method because value types can never be null and calling the static Equals method would box both value type objects.

Implementing Equals for a Reference Type When One or More of Its Base Classes Overrides Object's Equals

The following code shows how to implement Equals for a type that inherits an implementation of Equals other than the one Object provides:

// This is a reference type (because of 'class').
class MyRefType : BaseType {
  RefType refobj;  // This field is a reference type.
  ValType valobj;  // This field is a value type.

  public override Boolean Equals(Object obj) {
   // Let the base type compare its fields.
   if (!base.Equals(obj)) return false;

   // All the code from here down is identical to
   // that shown in the previous version.

   // Because 'this' isn't null, if obj is null, 
   // then the objects can't be equal.
   // NOTE: This line can be deleted if you trust that
   // the base type implemented Equals correctly.
   if (obj == null) return false;

   // If the objects are of different types, they can't be equal.
   // NOTE: This line can be deleted if you trust that
   // the base type implemented Equals correctly.
   if (this.GetType() != obj.GetType()) return false;

   // Cast obj to this type to access fields. NOTE: This cast
   // can't fail because you know that objects are of the same type.
   MyRefType other = (MyRefType) obj;

   // To compare reference fields, do this:
   if (!Object.Equals(refobj, other.refobj)) return false;

   // To compare value fields, do this:
   if (!valobj.Equals(other.valobj)) return false;

   return true;  // Objects are equal.
  }

  // Optional overloads of the == and != operators
  public static Boolean operator==(MyRefType o1, MyRefType o2) {
   if (o1 == null) return false;
   return o1.Equals(o2);
  }

  public static Boolean operator!=(MyRefType o1, MyRefType o2) {
   return !(o1 == o2);
  }
}

This code is practically identical to the code shown in the previous section. The only difference is that this version allows its base type to compare its fields, too. If the base type doesn't think the objects are equal, then they can't be equal.

It is very important that you not call base.Equals if this would result in calling the Equals method provided by System.Object. The reason is that Object's Equals method returns true only if the references point to the same object. If the references don't point to the same object, then false will be returned and your Equals method will always return false!

Certainly, if you're defining a type that is directly derived from Object, you should implement Equals as shown in the previous section. If you're defining a type that isn't directly derived from Object, you must first determine if that type (or any of its base types, except Object) provides an implementation of Equals. If any of the base types provides an implementation of Equals, then call base.Equals as shown in this section.

Implementing Equals for a Value Type

All value types are derived from System.ValueType. ValueType overrides the implementation of Equals offered by System.Object. Internally, System.ValueType's Equals method uses reflection to get the type's instance fields and compares the fields of both objects to see if they have equal values. This process is very slow, but it's a reasonably good default implementation that all value types will inherit. However, it does mean that reference types inherit an implementation of Equals that is really identity and that value types inherit an implementation of Equals that is value equality.

For value types that don't explicitly override Equals, the implementation provided by ValueType is inherited. The following code shows how System.-ValueType's Equals method is essentially implemented:

class ValueType {
  public override Boolean Equals(Object obj) {

   // Because 'this' isn't null, if obj is null, 
   // then the objects can't be equal.
   if (obj == null) return false;

   // Get the type of 'this' object.
   Type thisType = this.GetType();

   // If 'this' and 'obj' are different types, they can't be equal.
   if (thisType != obj.GetType()) return false;

   // Get the set of public and private instance
   // fields associated with this type.
   FieldInfo[] fields = thisType.GetFields(BindingFlags.Public |
     BindingFlags.NonPublic | BindingFlags.Instance);

   // Compare each instance field for equality. 
   for (Int32 i = 0; i < fields.Length; i++) {

     // Get the value of the field from both objects.
     Object thisValue = fields[i].GetValue(this);
     Object thatValue = fields[i].GetValue(obj);

     // If the values aren't equal, the objects aren't equal.
     if (!Object.Equals(thisValue, thatValue)) return false;
   }

   // All the field values are equal, and the objects are equal.
   return true;
  }
  §
}

Even though ValueType offers a pretty good implementation for Equals that would work for most value types that you define, you should still provide your own implementation of Equals. The reason is that your implementation will perform significantly faster and will be able to avoid extra boxing operations.

The following code shows how to implement Equals for a value type:

// This is a value type (because of 'struct').
struct MyValType {
  RefType refobj;  // This field is a reference type.
  ValType valobj;  // This field is a value type.

  public override Boolean Equals(Object obj) {
   // If obj is not your type, then the objects can't be equal.
   if (!(obj is MyValType)) return false;

   // Call the type-safe overload of Equals to do the work.
   return this.Equals((MyValType) obj);

  }

  // Implement a strongly typed version of Equals.
  public Boolean Equals(MyValType obj) {
   // To compare reference fields, do this:
   if (!Object.Equals(this.refobj, obj.refobj)) return false;

   // To compare value fields, do this:
   if (!this.valobj.Equals(obj.valobj)) return false;

   return true;  // Objects are equal.
  }

  // Optionally overload operator==
  public static Boolean operator==(MyValType v1, MyValType v2) {
   return (v1.Equals(v2));
  }

  // Optionally overload operator!=
  public static Boolean operator!=(MyValType v1, MyValType v2) {
   return !(v1 == v2);
  }
}

For value types, the type should define a strongly typed version of Equals. This version takes the defining type as a parameter, giving you type safety and avoiding extra boxing operations. You should also provide strongly typed operator overloads for the == and != operators. The following code demonstrates how to test two value types for equality:

MyValType v1, v2;

// The following line calls the strongly typed version of
// Equals (no boxing occurs).
if (v1.Equals(v2)) { ... }

// The following line calls the version of
// Equals that takes an object (4 is boxed).
if (v1.Equals(4)) { ... }

// The following doesn't compile because operator== 
// doesn't take a MyValType and an Int32.
if (v1 == 4) { ... }

// The following compiles, and no boxing occurs.
if (v1 == v2) { ... }

Inside the strongly typed Equals method, the code compares the fields in exactly the same way that you'd compare them for reference types. Keep in mind that the code doesn't do any casting, doesn't compare the two instances to see if they're the same type, and doesn't call the base type's Equals method. These operations aren't necessary because the method's parameter already ensures that the instances are of the same type. Also, because all value types are immediately derived from System.ValueType, you know that your base type has no fields of its own that need to be compared.

You'll notice in the Equals method that takes an Object that I used the is operator to check the type of obj. I used is instead of GetType because calling GetType on an instance of a value type requires that the instance be boxed.

Summary of Implementing Equals and the ==/!= Operators

In this section, I summarize how to implement equality for your own types:

  • Compiler primitive types—Your compiler will provide implementations of the == and != operators for types that it considers primitives. For example, the C# compiler knows how to compare Object, Boolean, Char, Int16, Uint16, Int32, Uint32, Int64, Uint64, Single, Double, Decimal, and so on for equality. In addition, these types provide implementations of Equals, so you can call this method as well as use operators.

  • Reference types—For reference types that you define, override the Equals method and, in the method, do all the work necessary to compare object states and return. If your type doesn't inherit Object's Equals method, call the base type's Equals method. If you want, overload the == and != operators and have them call the Equals method to do the actual work of comparing the fields.

  • Value types—For your value types, define a type-safe version of Equals that does all the work necessary to compare object states and return. Implement the type unsafe version of Equals by having it call the type-safe Equals internally. You also should provide overloads of the == and != operators that call the type-safe Equals method internally.

Identity

The purpose of a type's Equals method is to compare two instances of the type and return true if the instances have equivalent states or values. However, it's sometimes useful to see whether two references refer to the same, identical object. To do this, System.Object offers a static method called ReferenceEquals, which is implemented as follows:

class Object {
  public static Boolean ReferenceEquals(Object objA, Object objB) {
   return (objA == objB);
  }
}

As you can plainly see, ReferenceEquals simply uses the == operator to compare the two references. This works because of rules contained within the C# compiler. When the C# compiler sees that two references of type Object are being compared using the == operator, the compiler generates IL code that checks whether the two variables contain the same reference.

If you're writing C# code, you could use the == operator instead of calling Object's ReferenceEquals method, if you prefer. However, you must be very careful. The == operator is guaranteed to check identity only if the variables on both sides of the == operator are of the System.Object type. If a variable isn't of the Object type and if that variable's type has overloaded the == operator, the C# compiler will produce code to call the overloaded operator's method instead. So, for clarity and to ensure that your code always works as expected, don't use the == operator to check for identity; instead, you should use Object's static ReferenceEquals method. Here's some code demonstrating how to use ReferenceEquals:

static void Main() {
  // Construct a reference type object.
  RefType r1 = new RefType();

  // Make another variable point to the reference object.
  RefType r2 = r1;

  // Do r1 and r2 point to the same object?
  Console.WriteLine(Object.ReferenceEquals(r1, r2)); // "True"

  // Construct another reference type object.
  r2 = new RefType();

  // Do r1 and r2 point to the same object?
  Console.WriteLine(Object.ReferenceEquals(r1, r2)); // "False"

  // Create an instance of a value type.
  Int32 x = 5;

  // Do x and x point to the same object?
  Console.WriteLine(Object.ReferenceEquals(x, x));  // "False"
  // "False" is displayed because x is boxed twice
  // into two different objects.
}

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020