You are on page 1of 31

DEV323: C# 3.

0
Anders Hejlsberg
Technical Fellow
Microsoft Corporation

The Evolution of C#

C# 3.0
Language Integrated Query

C# 2.0
Generics

C# 1.0
Managed Code

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

Language Integrated Query .

c. Extension methods Anonymous Object types initializers . c.C# 3.0 Language Innovations var contacts = Query expressions from c in customers where c.State == "WA" Expression Local variable select new { c.Name.State == "WA") .Where(c => c.Phone }.Name.Phone }). trees type inference Lambda Automatic expressions properties var contacts = Partial customers methods .Select(c => new { c.

var numbers = new int[] {1. Dictionary<int. var i = 5. “The type on the right hand side” . var d = 1. 2. var orders = new Dictionary<int.Local Variable Type Inference int i = 5.Order>(). string s = "Hello". 3}. var s = "Hello".0. int[] numbers = new int[] {1. 2. double d = 1.Order> orders = new Dictionary<int.0.Order>(). 3}.

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

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

FindAll( new Predicate<Customer>(StateEqualsWA) ).State == "WA". List<Customer> locals = customers. } } . } static bool StateEqualsWA(Customer c) { return c.Lambda Expressions public class MyClass { public static void Main() { List<Customer> customers = GetCustomerList().

List<Customer> locals = customers.FindAll( delegate(Customer c) { return c. } } .State == "WA". } ).Lambda Expressions public class MyClass { public static void Main() { List<Customer> customers = GetCustomerList().

} } Lambda expression .Lambda Expressions public class MyClass { public static void Main() { List<Customer> customers = GetCustomerList().State == "WA").FindAll(c => c. List<Customer> locals = customers.

Lambda Expressions and Extension Methods .

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

"One" }. "Zero" }.Collection Initializers Must implement Must have public IEnumerable Add method List<int> numbers = new List<int> { 1. { 1.Add(10). "Two" }. { 2. numbers. string> { { 0. 10. parameter Dictionary<int. Add can take numbers. . more than one numbers.Add(1). string> spellings = new Dictionary<int.Add(100). 100 }. { 3. List<int> numbers = new List<int>(). "Three" } }.

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

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

Phone = c.Name. public string Phone. } . + public class Contact { public string Name.Anonymous Types IEnumerable<Contact> phoneListQuery = from c in customers where c.Phone }.State == "WA" select new Contact { Name = c.

Name. XXX foreach (var entry in phoneListQuery) { Console. } . IEnumerable<XXX> public string Phone.Anonymous Types class XXX { public string Name.Phone }.State == "WA" select new { Name = c.WriteLine(entry. Console.Phone).Name). } var phoneListQuery = from c in customers where c. Phone = c.WriteLine(entry.

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

Parameter(typeof(Customer).Equal( Expression. typeof(Customer).Constant("WA") ). .Expression Trees Code as Data public delegate bool Predicate<T>(T item). "c").State == "WA". Expression<Predicate<Customer>> test = c => c.GetProperty("State")).Property(c. c). ParameterExpression c = Expression. Expression<Predicate<Customer>> test = Expression.Lambda<Predicate<Customer>>(expr. Expression. Expression expr = Expression.

Expression Trees .

Automatic properties public class Product { public string Name. } . public decimal Price.

} set { price = value. public string Name { get { return name. } } } . decimal price.Automatic properties public class Product { string name. } set { name = value. } } public decimal Price { get { return price.

} { set { □ = value. } } public decimal Price { get. set.Automatic properties private string □. public string Name { public class Product get { return □. } public string Name { get. } } Must have both get and set . set.

LINQ Technical Preview .

Name. c.State == "WA") . c.Name.Phone }). trees type inference Lambda Automatic expressions properties var contacts = Partial customers methods .State == "WA" Expression Local variable select new { c.C# 3.Phone }.Where(c => c.0 Language Innovations var contacts = Query expressions from c in customers where c.Select(c => new { c. Extension methods Anonymous Object types initializers .

10:15 Room 115 Room 112 Room 112 DEV223 DEVWD12 LINQ Overview Explore C# 16:00 .0 (Repeat) 17:45 .14:45 Room 112 Room 112 DEV216 DEV323 C# Tips and Tricks C# 3.microsoft.0 LINQ to SQL 14:15 .17:15 13:30 .15:30 9:00 .18:15 Room 112 Room 125 http://msdn.10:15 9:00 .com/netframework/future/linq .19:00 17:00 .More Information Tuesday Wednesday Thursday DEV222 DEV323 DEV306 C# Through the Ages C# 3.

.

.

.

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