Anonymous types.
- April 20, 2015
Details...
Examples... //Create a 'person' object using an anonymous type
var bob = new { Name = "Bob", Age = 37 };
bob in this case is the anonymous type, and the types of the properties are inferred by the value assigned. //display information (note here however that the ToString is not required as a ToString method is automatically defined and is the anonymous type)
Console.WriteLine("Bob:" + bob.ToString());
//the ToString() method would output the following
Bob: { Name = Bob, Age = 37 }
var steve = new { Name = "Steve", Age = 36 };
//the compiler is able to recognize the anonymous type for bob and steve are identical and of the same class by the fact that the properties and types were
identical for both bob and steve
//the anonymous type also creates an equals method that is capable of comparing all objects of this anonymous
type (so name and age properties will be compared)
Console.WriteLine(bob.Equals(steve) ? "equal" : "not equal"));
The output from the is "not equal".