SlideShare a Scribd company logo
What's New in C# 3.0?  Clint Edmonson Architect Evangelist Microsoft Corporation www.notsotrivial.net
Agenda C# Design Themes New Features in Action Summary For More Information… Questions
C# 3.0 - Design Themes Improves on C# 2.0 100% Backwards Compatible Language Integrated Query (LINQ)
New Features in Action
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
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() ); }
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() ); }
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() ); } }
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 ); }
Auto-Implemented Properties public class Employee { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
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();
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; } }
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() ) ); }
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() ) ); }
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
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
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
 

More Related Content

PDF
The Ring programming language version 1.10 book - Part 97 of 212
PPT
PPTX
Clean code slide
PDF
The Ring programming language version 1.5.4 book - Part 82 of 185
PDF
The Ring programming language version 1.4 book - Part 22 of 30
PDF
Swift 2
PDF
The Ring programming language version 1.5 book - Part 14 of 31
PDF
Amusing C#
The Ring programming language version 1.10 book - Part 97 of 212
Clean code slide
The Ring programming language version 1.5.4 book - Part 82 of 185
The Ring programming language version 1.4 book - Part 22 of 30
Swift 2
The Ring programming language version 1.5 book - Part 14 of 31
Amusing C#

What's hot (20)

PPTX
C#.net evolution part 2
PDF
The Ring programming language version 1.5.2 book - Part 79 of 181
PPTX
C#.net Evolution part 1
PDF
The Ring programming language version 1.2 book - Part 59 of 84
PDF
The Ring programming language version 1.3 book - Part 62 of 88
PDF
Swift Programming Language
ODP
Design Patterns in Ruby
PDF
Denis Lebedev, Swift
PDF
Introduction to Swift programming language.
PPTX
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
PPT
C# Variables and Operators
PDF
DIG1108 Lesson 6
PDF
C++ Course - Lesson 3
PPT
Developing iOS apps with Swift
PDF
Cocoa Design Patterns in Swift
PDF
The Swift Compiler and Standard Library
PDF
Quick swift tour
KEY
Let's build a parser!
C#.net evolution part 2
The Ring programming language version 1.5.2 book - Part 79 of 181
C#.net Evolution part 1
The Ring programming language version 1.2 book - Part 59 of 84
The Ring programming language version 1.3 book - Part 62 of 88
Swift Programming Language
Design Patterns in Ruby
Denis Lebedev, Swift
Introduction to Swift programming language.
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
C# Variables and Operators
DIG1108 Lesson 6
C++ Course - Lesson 3
Developing iOS apps with Swift
Cocoa Design Patterns in Swift
The Swift Compiler and Standard Library
Quick swift tour
Let's build a parser!
Ad

Viewers also liked (9)

PPT
Csharp4 arrays and_tuples
PPT
Csharp4 delegates lambda_and_events
PDF
Secure mvc application saineshwar
PPTX
C# language
PDF
Free MVC project to learn for beginners.
PPT
Csharp4 basics
PPTX
PPTX
Introduction to .NET Core & ASP.NET Core MVC
PPTX
Introduction to C# 6.0 and 7.0
Csharp4 arrays and_tuples
Csharp4 delegates lambda_and_events
Secure mvc application saineshwar
C# language
Free MVC project to learn for beginners.
Csharp4 basics
Introduction to .NET Core & ASP.NET Core MVC
Introduction to C# 6.0 and 7.0
Ad

Similar to Whats New In C# 3.0 (20)

PPT
PostThis
PDF
Create a C# applicationYou are to create a class object called “Em.pdf
PPT
Embedded Typesafe Domain Specific Languages for Java
PDF
Chapter 8 Inheritance
PPT
Library Website
PPT
Linq intro
PPT
Clean code _v2003
PPT
Lecture5
PDF
classes and objects.pdfggggggggffffffffgggf
PPT
Templates exception handling
PDF
For C# need to make these changes to this programm, httppastebin..pdf
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
PDF
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
PDF
Classes
PPT
Micro-ORM Introduction - Don't overcomplicate
DOCX
2. Create a Java class called EmployeeMain within the same project Pr.docx
PDF
polymorphism in c++ with Full Explanation.
PDF
Object Oriented Programming (OOP) using C++ - Lecture 5
PDF
how do i expand upon selectionSortType so that you are no longer bou.pdf
PPT
Library Project Phase 1
PostThis
Create a C# applicationYou are to create a class object called “Em.pdf
Embedded Typesafe Domain Specific Languages for Java
Chapter 8 Inheritance
Library Website
Linq intro
Clean code _v2003
Lecture5
classes and objects.pdfggggggggffffffffgggf
Templates exception handling
For C# need to make these changes to this programm, httppastebin..pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
Classes
Micro-ORM Introduction - Don't overcomplicate
2. Create a Java class called EmployeeMain within the same project Pr.docx
polymorphism in c++ with Full Explanation.
Object Oriented Programming (OOP) using C++ - Lecture 5
how do i expand upon selectionSortType so that you are no longer bou.pdf
Library Project Phase 1

More from Clint Edmonson (20)

PPTX
New Product Concept Design.pptx
PPTX
Lean & Agile Essentials
PPTX
MICROSOFT BLAZOR - NEXT GENERATION WEB UI OR SILVERLIGHT ALL OVER AGAIN?
PPTX
Flow, the Universe and Everything
PPTX
Application architecture jumpstart
PPTX
Code smells and Other Malodorous Software Odors
PPTX
State of agile 2016
PPTX
Lean & Agile DevOps with VSTS and TFS 2015
PPTX
Application Architecture Jumpstart
PPTX
Agile Metrics That Matter
PPTX
Advanced oop laws, principles, idioms
PPTX
Application architecture jumpstart
PPTX
ADO.NET Entity Framework
PPTX
Windows 8 - The JavaScript Story
PPTX
Windows Azure Jumpstart
PPTX
Introduction to Windows Azure Virtual Machines
PPTX
Peering through the Clouds - Cloud Architectures You Need to Master
PPTX
Architecting Scalable Applications in the Cloud
PPTX
Windows Azure jumpstart
PPTX
Windows Azure Virtual Machines
New Product Concept Design.pptx
Lean & Agile Essentials
MICROSOFT BLAZOR - NEXT GENERATION WEB UI OR SILVERLIGHT ALL OVER AGAIN?
Flow, the Universe and Everything
Application architecture jumpstart
Code smells and Other Malodorous Software Odors
State of agile 2016
Lean & Agile DevOps with VSTS and TFS 2015
Application Architecture Jumpstart
Agile Metrics That Matter
Advanced oop laws, principles, idioms
Application architecture jumpstart
ADO.NET Entity Framework
Windows 8 - The JavaScript Story
Windows Azure Jumpstart
Introduction to Windows Azure Virtual Machines
Peering through the Clouds - Cloud Architectures You Need to Master
Architecting Scalable Applications in the Cloud
Windows Azure jumpstart
Windows Azure Virtual Machines

Recently uploaded (20)

PPTX
SPARSH-SVNITs-Annual-Cultural-Fest presentation for orientation
PPT
business model and some other things that
PPTX
providenetworksystemadministration.pptxhnnhgcbdjckk
DOCX
Elisabeth de Pot, the Witch of Flanders .
PPTX
shbthd htsh htrw hw htr 5w h5e 54 y.pptx
PPTX
genderandsexuality.pptxjjjjjjjjjjjjjjjjjjjj
PDF
Commercial arboriculture Commercial Tree consultant Essex, Kent, Thaxted.pdf
PPTX
the-solar-system.pptxxxxxxxxxxxxxxxxxxxx
PPTX
the Honda_ASIMO_Presentation_Updated.pptx
PDF
Keanu Reeves Beyond the Legendary Hollywood Movie Star.pdf
PDF
Between the Reels and the Revolution Enzo Zelocchi’s Unscripted Path Through ...
PPTX
Understanding Colour Prediction Games – Explained Simply
PDF
How Old Radio Shows in the 1940s and 1950s Helped Ella Fitzgerald Grow.pdf
PDF
Songlyrics.net-website for lyrics song download
PPTX
BULAN K3 NASIONAL PowerPt Templates.pptx
PDF
Download FL Studio Crack Latest version 2025
PDF
MAGNET STORY- Coaster Sequence (Rough Version 2).pdf
PDF
My Oxford Year- A Love Story Set in the Halls of Oxford
PPTX
just letters randomized coz i need to up
PDF
Rare Big Band Arrangers Who Revolutionized Big Band Music in USA.pdf
SPARSH-SVNITs-Annual-Cultural-Fest presentation for orientation
business model and some other things that
providenetworksystemadministration.pptxhnnhgcbdjckk
Elisabeth de Pot, the Witch of Flanders .
shbthd htsh htrw hw htr 5w h5e 54 y.pptx
genderandsexuality.pptxjjjjjjjjjjjjjjjjjjjj
Commercial arboriculture Commercial Tree consultant Essex, Kent, Thaxted.pdf
the-solar-system.pptxxxxxxxxxxxxxxxxxxxx
the Honda_ASIMO_Presentation_Updated.pptx
Keanu Reeves Beyond the Legendary Hollywood Movie Star.pdf
Between the Reels and the Revolution Enzo Zelocchi’s Unscripted Path Through ...
Understanding Colour Prediction Games – Explained Simply
How Old Radio Shows in the 1940s and 1950s Helped Ella Fitzgerald Grow.pdf
Songlyrics.net-website for lyrics song download
BULAN K3 NASIONAL PowerPt Templates.pptx
Download FL Studio Crack Latest version 2025
MAGNET STORY- Coaster Sequence (Rough Version 2).pdf
My Oxford Year- A Love Story Set in the Halls of Oxford
just letters randomized coz i need to up
Rare Big Band Arrangers Who Revolutionized Big Band Music in USA.pdf

Whats New In C# 3.0

  • 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)
  • 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 = &quot;Bill&quot;; employee1.LastName = &quot;Gates&quot;; Console.WriteLine( employee1.ToString() ); // We can always add a parameterized constructor to simplify coding var employee2 = new Employee( 2, &quot;Steve&quot;, &quot;Balmer&quot; ); 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=&quot;Clint&quot;, LastName=&quot;Edmonson&quot; }; 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.  

Editor's Notes

  • #2: 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