Anonymous type and tuple

Amutha picture Amutha · Apr 10, 2010 · Viewed 15.7k times · Source

What is the difference between anonymous type and tuple?

Answer

Alex Dresko picture Alex Dresko · May 18, 2017

Just a little update to this answer since C# 7 is out in the wild. Tuples have super powers now and can sometimes replace anonymous types and classes. Take for example this method that accepts and returns tuples with named properties.

void Main()
{
    var result = Whatever((123, true));
    Debug.Assert(result.Something == 123);
    Debug.Assert(result.Another == "True");
}

(int Something, string Another) Whatever((int Neat, bool Cool) data)
{
    return (data.Neat, data.Cool.ToString());
}

That's cool.