5. New Features in C# 3.0 Local Variable Type Inference Object Initializers Collection Initializers Anonymous Types Auto-Implemented Properties Extension Methods Lambdas Query Expressions LINQ Partial Methods
6. Local Variable Type Inference private static void LocalVariableTypeInference() { // Using the new 'var' keyword you can declare variables without having // to explicity declare their type. At compile time, the compiler determines // the type based on the assignment. int x = 10; var y = x; // Since the type inference happens at compile time, you cannot declare // a 'var' without an assignment //var a; // Output the type name for y Console.WriteLine( y.GetType().ToString() ); }
7. Object Initializers private static void ObjectInitializers() { // Simplest way to create an object and set it's properties var employee1 = new Employee(); employee1.ID = 1; employee1.FirstName = "Bill"; employee1.LastName = "Gates"; Console.WriteLine( employee1.ToString() ); // We can always add a parameterized constructor to simplify coding var employee2 = new Employee( 2, "Steve", "Balmer" ); Console.WriteLine( employee2.ToString() ); // New way to create object, providing all the property value assignments // Works with any publicly accessible properties and fields var employee3 = new Employee() { ID=3, FirstName="Clint", LastName="Edmonson" }; Console.WriteLine( employee3.ToString() ); }
8. Collection Initializers private static void CollectionInitializers() { // Create a prepopulated list var employeeList = new List<Employee> { new Employee { ID=1, FirstName="Bill", LastName="Gates" }, new Employee { ID=2, FirstName="Steve", LastName="Balmer" }, new Employee { ID=3, FirstName="Clint", LastName="Edmonson" } }; // Loop through and display contents of list foreach( var employee in employeeList ) { Console.WriteLine( employee.ToString() ); } }
9. Anonymous Types private static void AnonymousTypes() { var a = new { Name = "A", Price = 3 }; Console.WriteLine( a.GetType().ToString() ); Console.WriteLine( "Name = {0} : Price = {1}", a.Name, a.Price ); }
10. Auto-Implemented Properties public class Employee { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
11. Extension Methods public static class StringExtensionMethods { // NOTE: When using an extension method to extend a type whose source // code you cannot change, you run the risk that a change in the implementation // of the type will cause your extension method to break. // // If you do implement extension methods for a given type, remember the following // two points: // - An extension method will never be called if it has the same signature // as a method defined in the type. // - Extension methods are brought into scope at the namespace level. For example, // if you have multiple static classes that contain extension methods in a single // namespace named Extensions, they will all be brought into scope by the using // Extensions; namespace. // public static int WordCount( this String str ) { return str.Split( new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries ).Length; } } // Usage string s = "Hello Extension Methods"; int i = s.WordCount();
12. Partial Methods // Employee.cs public partial class Employee { public bool Terminated { get { return this.terminated; } set { this.terminated = value; this.OnTerminated(); } } private bool terminated; } // Employee.Customization.cs public partial class Employee { // If this method is not implemented // compiler will ignore calls to it partial void OnTerminated() { // Clear the employee's ID number this.ID = 0; } }
13. LINQ Expressions private static void LinqExpressions() { // Create a list of employees var employeeList = new List<Employee> { new Employee { ID=1, FirstName="Bill", LastName="Gates" }, new Employee { ID=2, FirstName="Steve", LastName="Balmer" }, new Employee { ID=3, FirstName="Clint", LastName="Edmonson" } }; // Search the list for founders using a lambda expression var foudersByLambda = employeeList.FindAll( employee => (employee.ID == 1 || employee.ID == 2) ); Console.WriteLine( foudersByLambda.Count.ToString() ); // Display collection using a lambda expression foudersByLambda.ForEach( employee => Console.WriteLine( employee.ToString() ) ); }
14. Query Expressions & Expression Trees private static void QueriesAndExpressions() { // Create a list of employees var employeeList = new List<Employee> { new Employee { ID=1, FirstName="Bill", LastName="Gates" }, new Employee { ID=2, FirstName="Steve", LastName="Balmer" }, new Employee { ID=3, FirstName="Clint", LastName="Edmonson" } }; // Retrieve the founders via a LINQ query var query1 = from employee in employeeList where employee.ID == 1 || employee.ID == 2 select employee; var founders = query1.ToList<Employee>(); founders.ForEach( founder => Console.WriteLine( founder.ToString() ) ); // Retrieve the new hires via a LINQ query that returns an anonymous type var query2 = from employee in employeeList where employee.ID == 3 select new { employee.FirstName, employee.LastName }; var newHires = query2.ToList(); newHires.ForEach( newHire => Console.WriteLine( newHire.ToString() ) ); }