Anonymous Type Verses Strongly Typed
// E.g., a strongly typed object versus using an anonymous type.
//A1. As a traditional strongly typed class and instance of that class.
public class Book
{
public int BookID { get; set; }
public string BookName { get; set; }
public string AuthorName { get; set; }
public string ISBN { get; set; }
}
//A2. The Instance
class BookApp
{
static void Main(string[] args)
{
Book book = new Book
{
BookID = 1,
BookName = "MVC Music Store Tutorial",
AuthorName = "Jon Galloway",
ISBN = "NA"
};
Console.WriteLine(book.BookName);
//B. Anonymous Type -- Object on the quick.
var novel = new { BookID = 1, BookName ="MVC Novel", AuthorName = "Mickey Mouse", ISBN = "NA" };
Console.WriteLine(novel.BookName);
//C. Anonymous Type Object with a Collection Type for one of its Properties.
var novella = new { BookID = 1, BookDetails = new List<Book>() {
new Book { BookName = "War and Peace", AuthorName = "Tolstoy", ISBN ="ds3435"},
new Book { BookName = "Old Yeller", AuthorName = "Gipson", ISBN ="ds4863"},
new Book { BookName = "The Hobbit", AuthorName = "Tolkein", ISBN ="ds6454"}
} };
foreach ( Book entry in novella.BookDetails)
{
Console.WriteLine(entry);
}
}
}