The world of .NET and Web Programming

C# 4.0/BCL 4 Series: StructuralComparisons Type

I have been spending quite a bit of time re-focusing and reviewing C# (and some BCL) fundamentals, with an eye to what's different in C# 4.0/BCL 4.0. The reason for this, is though C# 1.0 was so simple in it's day that it only required a 28 page spec, C#3 and 4 have really gotten away from me such that I'm only making average use of 3.0. I'll write a comparison review of the books I have been using, but for my money, the best and definitive book (as it was in 3.0) is C# 4.0 In A Nutshell (Disclaimer: I was a reviewer for V3 of this book and mentioned in the acknowledgements for this edition as well.

So back, to this cool new BCL 4.0 thing. This comes write out of the book on Page 275. As we all know, arrays are reference types, so that the statement arrayb = arraya results in two variables that reference the same array. Simarly, two distinct arrays will always fail an equality test - unless you use a custom equality comparer. Framework 4.0 provides one for the purpose of comparing elements in arrays or tuples which you can access via the StructuralComparisons type:

object[] a1 = {"string", 123, true};

object[] a2 = {"string", 123, true};

Console.WriteLine(a1 == a2); // False

Console.WriteLine(a1.Equals(a2)); // False

IStructuralEquatable se1 = a1;

Console.WriteLine(se1.Equals(a2,

 StructuralComparisons.StructuralEqualityComparer));

 

» Similar Posts

  1. C# 4.0/BCL 4: What's New Series
  2. C# 4.0/BCL 4 Series: System.Tuple
  3. C# 4.0/BCL 4 Series:Dynamic Primitive Type Part 2

» Trackbacks & Pingbacks

  1. Pingback from Twitter Trackbacks for Framework 4.0: StructuralComparisons Type : Sam Gentile's Blog (if (DeveloperTask == Communication && OS == Windows) [samgentile.com] on Topsy.com

  2. Pingback from Twitter Trackbacks for Framework 4.0: StructuralComparisons Type : Sam Gentile's Blog (if (DeveloperTask == Communication && OS == Windows) [samgentile.com] on Topsy.com

  3. Pingback from Twitter Trackbacks for Framework 4.0: StructuralComparisons Type : Sam Gentile's Blog (if (DeveloperTask == Communication && OS == Windows) [samgentile.com] on Topsy.com

  4. Might as well make this a series. I mentioned in the first post , "I have been spending quite a bit of time re-focusing and reviewing C# (and some BCL) fundamentals, with an eye to what's different in C# 4.0/BCL 4.0. The reason for this, is though

    C# 4.0/BCL 4: What's New Series — May 10, 2010 2:06 AM
Trackback link for this post:
http://samgentile.com/Web/trackback.ashx?id=1901

» Comments

  1. Krishna avatar

    LINQ Enumerable.SequenceEqual() looks easier.

    Krishna — May 10, 2010 11:06 AM

» Leave a Comment