Look at simple source code to learn few of new features of C# 3.0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public class Book { //Auto implemented properties public string Name { get; set; } public int ID { get; set; } } public class AllLINQ { public static void ShowAll() { //Collection Initializer and Implicitly Typed Local Variables using 'var'. //Note how List<Book> is created and filled in one line of source code. var Books = new List<Book> { new Book { Name = "Learn C#", ID = 1 }, new Book { Name = "Learn .Net", ID = 2 }, new Book { Name = "Power Electronics", ID = 3 } }; //LINQ var BooksQuery = from book in Books //calling extesion method where book.IsLearningSeries() //Anonymous Type select new { FetchedBook=book, TimeStamp=System.DateTime.Now }; foreach (var SearchedResult in BooksQuery) { Console.WriteLine(" Searched at : " + SearchedResult.TimeStamp); Console.WriteLine(" Book Name : " + SearchedResult.FetchedBook.Name + ", Book ID = " + SearchedResult.FetchedBook.ID); } } } public static class Extensions { //Extension Method for Book class. Note 'this' against parameter. public static bool IsLearningSeries(this Book b) { return b.Name.Contains("Learn"); } } |











Post a Comment