Archive

Posts Tagged ‘anonymous’

C# Tuples vs Anonymous Types

December 20, 2010 Leave a comment

Since I started learning Functional Programming with Scala, I have been more interested in other languages that can do the same i.e. let you write code in FP style. Thanks to Micrsoft for including FP style in their all time famous language C#. I have never used VB and therefore I am not sure how FP style works in VB. I am not sure why Sun/Oracle is still behind in adding FP capabilities in Java.

Ok let’s cut short to the main point. I liked the idea of anonymous types in FP languages. It makes the code succinct. Instead of designing a class with a constructor and private members with public accessors, we can have Tuples. I came across this blog when I searched “C# Tuples” — http://sankarsan.wordpress.com/2009/11/29/tuple-in-c-4-0/
I was not that impressed. It seemed like the same old way of doing things.
But then I came across anonymous types that can be declared as follows:


var v = new {Name = "John", MiddleInitial = 'D', Age = 25, Amount = 2.55f, Pet = new {Name = "Sparky", Breed = "Chihuahua", Age = 10} }

Isn’t that a better way than using Tuples? Please, let me know through comments if Tuples is a better than this.