0

I have no clue what an "anonymous type" is in C# nor how it is used. Can somone give me a good description of it and it's use?

[Note: i really know what it is and how to use it but thought i'd ask for those that don't]

RCIX
  • 38,647
  • 50
  • 150
  • 207
  • does that count as rep trolling? – Jherico Oct 28 '09 at 06:16
  • Those that don't can google it and when they give up they can create a question. Why are you asking it if it doesn't benefit you? – vanja. Oct 28 '09 at 06:30
  • @vanja, @Jherico: Do you go around commenting similar questions from when SO first opened? – jasonh Oct 28 '09 at 06:42
  • @jasonh: Ignoring the fact that SO is different now than it was when it first started; yes, I think its nothing more than rep trolling to ask questions when you do not care about the answers. – vanja. Oct 30 '09 at 02:00
  • I DO care about the answers, i asked because i was trying to answer someone elses question and didn't see a question like this. – RCIX Oct 30 '09 at 03:33

3 Answers3

7

An anonymous type is a type generated by the compiler due to an expression such as:

new { Property1 = x.Value1, Property2 = y.Value2, z.Value3 }

(the last one is like Value3 = z.Value3).

The name of the anonymous type is "unspeakable" - i.e. you can't specify it in normal C# - but it's a perfectly normal type as far as the CLR is concerned. As you can't write the name, if you want to create a variable of an anonymous type (or a generic type using an anonymous type as the type argument), you need to use an implicitly typed local variable with the var keyword:

var person = new { Name = "Bill", Address = "..." };

C# anonymous types are immutable (i.e. the properties are read-only) - the generated type has a single constructor which takes values for all the properties as parameters. The property types are inferred from the values.

Anonymous types override GetHashCode, Equals and ToString in reasonably obvious ways - the default equality comparer for each property type is used for hashing and equality.

They are typically used in LINQ in the same way that you'd use "SELECT Value1 As Property1, Value2 As Property2, Value3" in SQL.

Every anonymous type initializer expression which uses the same property names and types in the same order will refer to the same type, so you can write:

var x = new { Name = "Fred", Age = 10 };
x = new { Name = "Bill", Age = 15 };

It's also worth knowing that VB anonymous types are slightly different: by default, they're mutable. You can make each individual property immutable using the "Key" keyword. Personally I prefer the C# way, but I can see mutability being useful in some situations.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You learn something new every day. I had no idea they were immutable and override `GetHashcode`, `Equals` and `ToString`. What do they override ToString with? – jasonh Oct 28 '09 at 06:46
  • 2
    @jasonh: Comma separated values in braces, e.g. { X = 2, Y = 10 } – Jon Skeet Oct 28 '09 at 07:05
  • Uh oh. Someone has hacked Jon Skeet's account and stolen his rep and badges and stuff! ;) – RCIX Nov 03 '09 at 02:44
0

An anonymous type is a type that has no name. You can use it anywhere you don't need the name of the type. For instance:

var query = from x in set where x.Property1 = value select new {x.Property1, x.Property2};
foreach (var q in query) {
    // do something with q.Property1, q.Property2
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397