Anonymous types are real types - the actual name of the type is not known (at least to you as the developer). The name of the type will be known to the compiler but you will not be able to access it. In an anonymous type you will define properties, which will be read-only and the type of each property will be inferred from the data put into it. Let's look at an example
var book = new { BookName = "Twilight", Author = "Stephenie Meyer", ISBN = "0316160172" }; Console.WriteLine("Name {0}", book.BookName); Console.WriteLine("Author {0}", book.Author); Console.WriteLine("ISBN {0}", book.ISBN); Console.WriteLine(book); Console.WriteLine(book.GetType());Here we create a variable called book which is output using the properties (BookName, Author and ISBN). We also output the variable and the type that it is called.
The output of book (using the ToString() method) is useful for debugging. Finally the type name is not useful - but internally this is what the compiler will store it as.
Looking at the code we are using this var keyword. A variable declared with the var keyword is still strongly typed - but the compiler will determine the type. If we have a List
List<string> inputs = new List<string> { "one", "two", "three", "four", "five" }; foreach (var input in inputs) Console.WriteLine(input);Here the use of the var keyword is allowed - but the type is known. I have begun to see developers using the var keyword all the time - rather than in this case use the string keyword. You know what the type is - why make it difficult for other developers reading your code! Just because you can do something doesn't mean you have to do it.
But in our example to create an instance of our anonymous type we used var. We are forced to use var as we don't know what type we have.
In LINQ we generally don't know what type we are creating. Let's look at the LINQ example to convert the strings to upper case. I am using var here - but what type is being returned?
var outputs = from s in inputs select s.ToUpper(); foreach (var output in outputs) Console.WriteLine(output);The output is IEnumerable<string> - this is not an anonymous type. We know that when the LINQ query is executed (when it is first accessed) it is going to return something of this type. But, by convention we use the var keyword.
Another common thing to do is the following
var outputs = (from s in inputs select s.ToUpper()).ToList();This forces immediate execution - but the type is known, it is List<string>. But, again, by convention we use the var keyword.
Here is an example with an anonymous type.
var infos = from s in inputs select new { Original = s, Upper = s.ToUpper(), Lower = s.ToLower(), Length = s.Length }; foreach (var info in infos) Console.WriteLine(info);In this example the output will be an anonymous type - with four properties (Original, Upper, Lower and Length). In this case we are forced to use the var keyword. The output from this looks like
As I said earlier a variable declared with the var keyword is still strongly typed. You will find that the intellisense works.