Created
May 27, 2015 21:59
-
-
Save csdear/fc7910d0db3b1030a9f2 to your computer and use it in GitHub Desktop.
Anonymous Type Verses Strongly Typed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment