15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

148 ❘ ChaPTer 6 ArrAys And tuples<br />

}<br />

}<br />

this.LastName == other.LastName;<br />

code snippet StructuralComparison/Person.cs<br />

Now two arrays containing Person items are created. Both arrays contain the same Person object with<br />

the variable name janet, <strong>and</strong> two different Person objects that have the same content. The comparison<br />

operator != returns true because there are indeed two different arrays referenced from two variable<br />

names persons1 <strong>and</strong> persons2. Because the Equals() method with one parameter is not overridden<br />

by the Array class, the same happens as with the == operator to compare the references, <strong>and</strong> they are not<br />

the same:<br />

var janet = new Person { FirstName = "Janet", LastName = "Jackson" };<br />

Person[] persons1 = {<br />

new Person<br />

{<br />

FirstName = "Michael",<br />

LastName = "Jackson"<br />

},<br />

janet<br />

};<br />

Person[] persons2 = {<br />

new Person<br />

{<br />

FirstName = "Michael",<br />

LastName = "Jackson"<br />

},<br />

janet<br />

};<br />

if (persons1 != persons2)<br />

Console.WriteLine("not the same reference");<br />

code snippet StructuralComparison/Program.cs<br />

Invoking the Equals() method defined by the IStructuralEquatable that is the method with the<br />

first parameter of type object <strong>and</strong> the second parameter of type IEqualityComparer, you can define<br />

how the comparison should be done by passing an object that implements IEqualityComparer. A<br />

default implementation of the IEqualityComparer is done by the EqualityComparer class. This<br />

implementation checks if the type implements the interface IEquatable, <strong>and</strong> invokes the IEquatable.<br />

Equals() method. If the type does not implement IEquatable, the Equals() method from the base class<br />

Object is invoked to do the comparison.<br />

Person implements IEquatable, where the content of the objects is compared <strong>and</strong> the arrays<br />

indeed contain the same content:<br />

if ((persons1 as IStructuralEquatable).Equals(persons2,<br />

EqualityComparer.Default))<br />

{<br />

Console.WriteLine("the same content");<br />

}<br />

You’ll see next how the same thing can be done with tuples. Here, two tuple instances are created that<br />

have the same content. Of course, because the references t1 <strong>and</strong> t2 reference two different objects,<br />

the comparison operator != returns true:<br />

var t1 = Tuple.Create(1, "Stephanie");<br />

var t2 = Tuple.Create(1, "Stephanie");<br />

if (t1 != t2)<br />

Console.WriteLine("not the same reference to the tuple");<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!