if (obj == null) return false;

  //---see if obj can be cast to Contact---

  Contact c = obj as Contact;

  if ((System.Object)c == null) return false;

  //---check individual fields---

  return

   (ID == c.ID) && (FirstName == c.FirstName) &&

   (LastName == c.LastName) && (Email == c.Email);

 }

 public bool Equals(Contact c) {

  //---check for null obj---

  if (c == null) return false;

  //---check individual fields---

  return

   (ID == c.ID) && (FirstName == c.FirstName) &&

   (LastName == c.LastName) && (Email == c.Email);

 }

 public override int GetHashCode() {

  return ID;

 }

}

Essentially, you're adding the following:

□ The Equals(object obj) method to override the Equals() virtual method in the System.Object class. This method takes in a generic object (System.Object) as argument.

□ The Equals(Contact c) method to test for value equality. This method is similar to the first method, but it takes in a Contact object as argument.

□ The GetHashCode() method to override the GetHashCode() virtual method in the System.Object class.

The as Keyword

In the Equals(object obj) method you saw the use of the as keyword:

Contact c = obj as Contact;

The as operator performs conversions between compatible types. In this case, it tries to cast the obj object into a Contact object. The as keyword is discussed in detail in Chapter 5.

Notice that the Equals() methods essentially performs the following to determine if two objects are equal in value:

□ It checks whether the object passed is in null. If it is, it returns false.

□ It checks whether the object passed is a Contact object (the second Equals() method need not check for this). If it isn't, it returns false.

□ Last, it checks to see whether the individual members of the passed-in Contact object are of the same value as the members of the current object. Only when all the members have the same values (which members to test are determined by you) does the Equals() method return true. In this case, all the four members' values must be equal to the passed-in Contact object.

The following statement will now print out True:

Console.WriteLine(c1.Equals(c2)); //---True---

ToString() Method

All objects in C# inherits the ToString() method, which returns a string representation of the object. For example, the DateTime class's ToString() method returns a string containing the date and time, as the following shows:

DateTime dt = new DateTime(2008, 2, 29);

//---returns 2/29/2008 12:00:00 AM---

Console.WriteLine(dt.ToString());

For custom classes, you need to override the ToString() method to return the appropriate string. Using the example of the Contact class, an instance of the Contact class's ToString() method simply returns the string 'Contact':

Contact c1 = new Contact() {

 ID = 1234,

 FirstName = 'Wei-Meng',

 LastName = 'Lee',

 Email = '[email protected]'

};

//---returns 'Contact'---

Console.WriteLine(c1.ToString());

This is because the ToString() method from the Contact class inherits from the System.Object class, which simply returns the name of the class.

To ensure that the ToString() method returns something appropriate, you need to override it:

class Contact {

 public int ID;

 public string FirstName;

 public string LastName;

 public string Email;

 public override string ToString() {

  return ID + ',' + FirstName + ',' + LastName + ',' + Email;

 }

 //...

}

In this implementation of the ToString() method, you return the concatenation of the various data members, as evident in the output of the following code:

Contact c1 = new Contact() {

 ID = 1234,

 FirstName = 'Wei-Meng',

 LastName = 'Lee',

 Email = '[email protected]'

};

//---returns '1234,Wei-Meng,Lee,[email protected]' ---

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату