The second statement is straightforward, as c1
and c3
are two different objects:
Console.WriteLine(c1.Equals(c3)); //---False---
The third and fourth statements assign c1
to c3
, which means that c1
and c3
are now two different variables pointing to the same object. Hence, Equals()
returns True
:
c3 = c1;
Console.WriteLine(c1.Equals(c3)); //---True---
The fifth and sixth statements test the reference equality of c1
against c2
and then c1
against c3
:
Console.WriteLine(Object.ReferenceEquals(c1, c2)); //---False---
Console.WriteLine(Object.ReferenceEquals(c1, c3)); //---True---
If two objects have reference equality, they also have value equality, but the reverse is not necessarily true.
By default the Equals()
method tests for reference equality. To ensure that it tests for value equality rather than reference equality, you need to override the Equals()
virtual method.
Using the same Contact
class used in the previous section, add the methods highlighted in the following code:
public class Contact {
public int ID;
public string FirstName;
public string LastName;
public string Email;
public override bool Equals(object obj) {
//---check for null obj---
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---
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 = "weimenglee@learn2develop.net"
};
//---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 = "weimenglee@learn2develop.net"
};
//---returns "1234,Wei-Meng,Lee,weimenglee@learn2develop.net" ---
Console.WriteLine(c1.ToString());
Attributes are descriptive tags that can be used to provide additional information about types (classes), members, and properties. Attributes can be used by .NET to decide how to handle objects while an application is running.
There are two types of attributes:
□ Attributes that are defined in the CLR.
□ Custom attributes that you can define in your code.
CLR Attributes
Consider the following Contact
class definition:
class Contact {
public string FirstName;
public string LastName;
public void PrintName() {
Console.WriteLine("{0} {1}", this.FirstName, this.LastName);
}
[Obsolete("This method is obsolete. Please use PrintName()")]
Читать дальше