Whats New In C# 3.0
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Whats New In C# 3.0

on

  • 1,379 views

 

Statistics

Views

Total Views
1,379
Views on SlideShare
1,368
Embed Views
11

Actions

Likes
0
Downloads
31
Comments
0

2 Embeds 11

http://www.notsotrivial.net 10
http://www.linkedin.com 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • MGB 2003 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary. Hi, my name is Clint Edmonson Architect Evangelist based in the United States in St. Louis, Missouri. Today we’ll be talking about all the cool new language features in C# 3.0

Whats New In C# 3.0 Presentation Transcript

  • 1. What's New in C# 3.0? Clint Edmonson Architect Evangelist Microsoft Corporation www.notsotrivial.net
  • 2. Agenda
    • C# Design Themes
    • New Features in Action
    • Summary
    • For More Information…
    • Questions
  • 3. C# 3.0 - Design Themes Improves on C# 2.0 100% Backwards Compatible Language Integrated Query (LINQ)
  • 4. New Features in Action
  • 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=&quot;Bill&quot;, LastName=&quot;Gates&quot; }, new Employee { ID=2, FirstName=&quot;Steve&quot;, LastName=&quot;Balmer&quot; }, new Employee { ID=3, FirstName=&quot;Clint&quot;, LastName=&quot;Edmonson&quot; } }; // 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 = &quot;A&quot;, Price = 3 }; Console.WriteLine( a.GetType().ToString() ); Console.WriteLine( &quot;Name = {0} : Price = {1}&quot;, 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 = &quot;Hello Extension Methods&quot;; 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=&quot;Bill&quot;, LastName=&quot;Gates&quot; }, new Employee { ID=2, FirstName=&quot;Steve&quot;, LastName=&quot;Balmer&quot; }, new Employee { ID=3, FirstName=&quot;Clint&quot;, LastName=&quot;Edmonson&quot; } }; // 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=&quot;Bill&quot;, LastName=&quot;Gates&quot; }, new Employee { ID=2, FirstName=&quot;Steve&quot;, LastName=&quot;Balmer&quot; }, new Employee { ID=3, FirstName=&quot;Clint&quot;, LastName=&quot;Edmonson&quot; } }; // 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() ) ); }
  • 15. Best Practices
    • Features are listed in increasing complexity
    • Don’t use features because they are new or cool
    • Leverage new features to improve code readability and maintainability
    • Decide as a team to start using new features and use them consistently
  • 16. For More Information…
    • Visual C# Developer Center
      • http://msdn2.microsoft.com/en-us/vcsharp/default.aspx
    • Accelerated C# 2008 by Trey Nash (Apress 2007)
    • Continue the conversation on my blog: www.notsotrivial.net
  • 17. Questions and Answers
    • Submit text questions using the “Ask” button.
    • Don’t forget to fill out the survey.
    • For upcoming and previously live webcasts: www.microsoft.com/webcast
    • Got webcast content ideas? Contact us at: http://go.microsoft.com/fwlink/?LinkId=41781
  • 18.