Presentation is loading. Please wait.

Presentation is loading. Please wait.

Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation

Similar presentations


Presentation on theme: "Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation"— Presentation transcript:

1 Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation rajpai@microsoft.com

2 Satisfy Your Technical Curiosity The Evolution of C# C# 1.0 C# 2.0 C# 3.0 Managed Code Generics Language Integrated Query

3 Satisfy Your Technical Curiosity C# 3.0 Design Goals Integrate objects, relational data, and XML Increase conciseness of language Add functional programming constructs Don’t tie language to specific APIs Remain 100% backwards compatible

4 Satisfy Your Technical Curiosity

5 Lambda Expressions public delegate bool Predicate (T obj); public class List { public List FindAll(Predicate test) { List result = new List (); foreach (T item in this) if (test(item)) result.Add(item); return result; } … }

6 Satisfy Your Technical Curiosity Lambda Expressions public class MyClass { public static void Main() { List customers = GetCustomerList(); List locals = customers.FindAll( new Predicate (StateEqualsWA) ); } static bool StateEqualsWA(Customer c) { return c.State == "WA"; }

7 Satisfy Your Technical Curiosity Lambda Expressions public class MyClass { public static void Main() { List customers = GetCustomerList(); List locals = customers.FindAll( delegate(Customer c) { return c.State == "WA"; } ); }

8 Satisfy Your Technical Curiosity Lambda Expressions public class MyClass { public static void Main() { List customers = GetCustomerList(); List locals = customers.FindAll(c => c.State == "WA"); } Lambda expression

9 Satisfy Your Technical Curiosity Extension Methods

10 Satisfy Your Technical Curiosity Extension Methods namespace MyStuff { public static class Extensions { public static string Concatenate(this IEnumerable strings, string separator) {…} } using MyStuff; string[] names = new string[] { "Axel", "Mia", "Niels" }; string s = names.Concatenate(", "); Extension method Brings extensions into scope obj.Foo(x, y)  XXX.Foo(obj, x, y) IntelliSense!

11 Satisfy Your Technical Curiosity Object Initializers

12 Satisfy Your Technical Curiosity Object Initializers public class Point { private int x, y; public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } } Point a = new Point { X = 0, Y = 1 }; Point a = new Point(); a.X = 0; a.Y = 1; Field or property assignments

13 Satisfy Your Technical Curiosity Collection Initializers List numbers = new List { 1, 10, 100 }; Must implement IEnumerable List numbers = new List (); numbers.Add(1); numbers.Add(10); numbers.Add(100); Must have public Add method Dictionary spellings = new Dictionary { { 0, "Zero" }, { 1, "One" }, { 2, "Two" }, { 3, "Three" } }; Add can take more than one parameter

14 Satisfy Your Technical Curiosity Anonymous Types

15 Satisfy Your Technical Curiosity IEnumerable phoneListQuery = from c in customers where c.State == "WA" select new Contact { Name = c.Name, Phone = c.Phone }; Anonymous Types public class Contact { public string Name; public string Phone; } +

16 Satisfy Your Technical Curiosity var phoneListQuery = from c in customers where c.State == "WA" select new { Name = c.Name, Phone = c.Phone }; Anonymous Types class XXX { public string Name; public string Phone; } IEnumerable foreach (var entry in phoneListQuery) { Console.WriteLine(entry.Name); Console.WriteLine(entry.Phone); } XXX

17 Satisfy Your Technical Curiosity Local Variable Type Inference

18 Satisfy Your Technical Curiosity Local Variable Type Inference int i = 5; string s = "Hello"; double d = 1.0; int[] numbers = new int[] {1, 2, 3}; Dictionary orders = new Dictionary (); var i = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary (); “The type on the right hand side”

19 Satisfy Your Technical Curiosity Query Expressions

20 Satisfy Your Technical Curiosity Query Expressions from id in source { from id in source | join id in source on expr equals expr [ into id ] | let id = expr | where condition | orderby ordering, ordering, … } select expr | group expr by key [ into id query ] Starts with from Zero or more from, join, let, where, or orderby Ends with select or group by Optional into continuation

21 Satisfy Your Technical Curiosity from c in customers where c.State == "WA" select new { c.Name, c.Phone }; customers.Where(c => c.State == "WA").Select(c => new { c.Name, c.Phone }); Query Expressions Queries translate to method invocations Where, Join, OrderBy, Select, GroupBy, …

22 Satisfy Your Technical Curiosity Expression Trees Code as Data Predicate test = c => c.State == "WA"; Predicate test = new Predicate (XXX); private static bool XXX(Customer c) { return c.State == "WA"; } public delegate bool Predicate (T item);

23 Satisfy Your Technical Curiosity Expression Trees Code as Data Expression > test = c => c.State == "WA"; public delegate bool Predicate (T item); ParameterExpression c = Expression.Parameter(typeof(Customer), "c"); Expression expr = Expression.Equal( Expression.Property(c, typeof(Customer).GetProperty("State")), Expression.Constant("WA") ); Expression > test = Expression.Lambda >(expr, c);

24 Satisfy Your Technical Curiosity Automatic Properties

25 Satisfy Your Technical Curiosity Automatic properties public class Product { public string Name; public decimal Price; }

26 Satisfy Your Technical Curiosity Automatic properties public class Product { string name; decimal price; public string Name { get { return name; } set { name = value; } } public decimal Price { get { return price; } set { price = value; } }

27 Satisfy Your Technical Curiosity Automatic properties public class Product { public string Name { get; set; } public decimal Price { get; set; } } private string □; public string Name { get { return □; } set { □ = value; } } Must have both get and set

28 Satisfy Your Technical Curiosity Partial Methods

29 Satisfy Your Technical Curiosity Partial Methods partial class Customer { public string Name { get { … } set { _name = value; }

30 Satisfy Your Technical Curiosity Partial Methods partial class Customer { public string Name { get { … } set { OnNameChanging(value); _name = value; OnNameChanged(); } partial void OnNameChanging(string value); partial void onNameChanged(); } Partial Method Call Partial Method Definition

31 Satisfy Your Technical Curiosity C# 3.0 Language Innovations var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone }; var contacts = customers.Where(c => c.State == "WA").Select(c => new { c.Name, c.Phone }); Extension methods Lambda expressions Query expressions Object initializers Anonymous types Local variable type inference Expression Trees Automatic Properties Partial Methods

32 Satisfy Your Technical Curiosity C# 3.0 Design Goals Integrate objects, relational data, and XML Increase conciseness of language Add functional programming constructs Don’t tie language to specific APIs Remain 100% backwards compatible

33 Satisfy Your Technical Curiosity

34 More Information WednesdayThursday DEV203 LINQ Overview 14:30 – 15:45 DEV307 C# 3.0 9:00 – 10:15 DEV205 Visual Studio Orcas 17:45 – 18:45 DEV204 ADO.NET vNext 10:45 – 12:00 DEV318 Visual Basic 9.0 13:00 – 14:15 http://csharp.net

35 Satisfy Your Technical Curiosity ©2006 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.


Download ppt "Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation"

Similar presentations


Ads by Google