Home > Programming > C# Tuples vs Anonymous Types

C# Tuples vs Anonymous Types

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.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: