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.

s u m m a r y ❘ 149<br />

The Tuple < > class offers two Equals() methods: one that is overridden from the Object base class<br />

with an object as parameter, <strong>and</strong> the second that is defi ned by the IStructuralEqualityComparer<br />

interface with object <strong>and</strong> IEqualityComparer as parameters. Another tuple can be passed to the fi rst<br />

method as shown. This method uses EqualityComparer < object > .Default to get an ObjectEqualityCom<br />

parer < object > for the comparison. This way every item of the tuple is compared by invoking the Object.<br />

Equals() method. If every item returns true , the fi nal result of the Equals() method is true , which is<br />

the case here with the same int <strong>and</strong> string values:<br />

if (t1.Equals(t2))<br />

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

You can also create a custom IEqualityComparer , as shown with the class TupleComparer . This class<br />

implements the two methods Equals() <strong>and</strong> GetHashCode() of the IEqualityComparer interface:<br />

class TupleComparer: IEqualityComparer<br />

{<br />

public new bool Equals(object x, object y)<br />

{<br />

return x.Equals(y);<br />

}<br />

}<br />

public int GetHashCode(object obj)<br />

{<br />

return obj.GetHashCode();<br />

}<br />

code snippet StructuralComparison/Program.cs<br />

The implementation of the Equals() method of the IEqualityComparer interface<br />

requires the new modifi er or an implicit interface implementation because the base<br />

class Object defi nes a static Equals() method with two parameters as well.<br />

The TupleComparer is used, passing a new instance to the Equals() method of the Tuple < T1, T2 > class.<br />

The Equals() method of the Tuple class invokes the Equals() method of the TupleComparer for every<br />

item to be compared. So with the Tuple < T1, T2 > class, the TupleComparer is invoked two times to check<br />

whether all items are equal:<br />

if (t1.Equals(t2, new TupleComparer()))<br />

Console.WriteLine("equals using TupleComparer");<br />

summary<br />

In this chapter, you ’ ve seen the <strong>C#</strong> notation to create <strong>and</strong> use simple, multidimensional, <strong>and</strong> jagged arrays.<br />

The Array class is used behind the scenes of <strong>C#</strong> arrays, <strong>and</strong> this way you can invoke properties <strong>and</strong> methods<br />

of this class with array variables.<br />

You ’ ve seen how to sort elements in the array by using the IComparable <strong>and</strong> IComparer interfaces. You ’ ve<br />

learned using <strong>and</strong> creating enumerators, the interfaces IEnumerable <strong>and</strong> IEnumerator , <strong>and</strong> the yield<br />

statement. You ’ ve seen tuples, a new feature of .<strong>NET</strong> 4.<br />

Moving on, the next chapter focuses on operators <strong>and</strong> casts.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!