c# 3.0 language innovations

Click here to load reader

Download C# 3.0 Language Innovations

Post on 26-Aug-2014

3.340 views

Category:

Technology

1 download

Embed Size (px)

DESCRIPTION

C# 3.0 Language Innovations in .NET 3.5 like:Language Innovations,Lambda Expressions, Extension Methods, Anonymous Types, Object Initializers, Collection Initializers, Local Variable Type Inference, Expression Trees, Query Expressions

TRANSCRIPT

  • C# 3.0 Language Innovations Query var CommonWords = expressions from w in wordOccurances where w.Count > 2 Local variable select new { f.Name, w.Word, W.Count }; type inference Lambda expressions var CommonWords = wordOccurances .Where(w => w.Count > 2) .Select(w => new {f.Name, w.Word, W.Count }); Extension methods Anonymous Object types initializers 1
  • Lambda Expressions public delegate bool Predicate(T obj); public class List { Statement public List FindAll(Predicate test) { } context Explicitly typed } Implicitly List customers = typed GetCustomerList(); List x = customers.FindAll( Expression delegate(Customer c) { return c.State == "WA"; } context ); List x = customers.FindAll(c => c.State == "WA"); 2
  • Lambda Expressions public delegate T Func(); public delegate T Func(A0 arg0); public delegate T Func(A0 arg0, A1 arg1); Func test = c => c.State == "WA"; double factor = 2.0; Func f = x => x * factor; Func f = (x, y) => x * y; Func comparer = (int x, int y) => { if (x > y) return 1; if (x < y) return -1; return 0; }; 3
  • Queries Through APIs Query operators are just methods public class List { public List Where(Func predicate) { } public List Select(Func selector) { } } Methods compose to form queries List customers = GetCustomerList(); List contacts = customers.Where(c => c.State == "WA").Select(c => c.Name); But what about other types? Declare operators in all collections? Type inference figures out What about arrays? 4
  • Queries Through APIs Query operators are static methods public static class Sequence { public static IEnumerable Where(IEnumerable source, Func predicate) { } public static IEnumerable Select(IEnumerable source, Func selector) { } } Huh? Customer[] customers = GetCustomerArray(); IEnumerable contacts = Sequence.Select( Sequence.Where(customers, c => c.State == "WA"), c => c.Name); Want methods on IEnumerable 5
  • Extension Methods class Customer { public string Name; } Customer[ ] c = new Customer[1]; c._ Huh ?!!?! 6
  • Extension Methods Extension namespace System.Query methods { public static class Sequence { public static IEnumerable Where(this IEnumerable source, Func predicate) { } public static IEnumerable Select(this IEnumerable source, obj.Foo(x, y) Func selector) { } Brings extensions into XXX.Foo(obj, x, y) } scope } using System.Query; IEnumerable contacts = customers.Where(c => c.State == "WA").Select(c => c.Name); IntelliSense! 7
  • Extension Methods public static class MyExtensions { public static IEnumerable Where( this IEnumerable source, Func predicate) { foreach (T item in source) if (predicate(item)) yield return item; } } 8
  • Extension Methods Method is named Where public static class MyExtensions { public static IEnumerable Where( this IEnumerable source, Func predicate) { foreach (T item in source) if (predicate(item)) yield return item; } } 9
  • Extension Methods Method is named Where public static class MyExtensions Method extends { objects of type IEnumerable public static IEnumerable Where( this IEnumerable source, Func predicate) { foreach (T item in source) if (predicate(item)) yield return item; } } 10
  • Extension Methods Method is named Where public static class MyExtensions Method extends { objects of type IEnumerable public static IEnumerable Where( this IEnumerable source, Func predicate) { foreach (T item in source) if (predicate(item)) yield return item; Implementation } } 11
  • Extension Methods Method is named Where public static class MyExtensions Method extends { objects of type IEnumerable public static IEnumerable Where( this IEnumerable source, Func predicate) Signature of the { predicate parameter foreach (T item in source) if (predicate(item)) yield return item; Implementation } } 12
  • Extension Methods Method is named Where public static class MyExtensions Method extends { objects of type IEnumerable public static IEnumerable Where( this IEnumerable source, Func predicate) Signature of the { predicate parameter foreach (T item in source) if (predicate(item)) yield return item; Implementation } } string[] names = { "Burke", "Connor", "Frank }; IEnumerable expr = MyExtensions.Where(names, s => s.Length < 6); 13
  • Extension Methods Method is named Where [System.Runtime.CompilerServices.Extension] public static class MyExtensions Method extends { objects of type IEnumerable public static IEnumerable Where( this IEnumerable source, Func predicate) Signature of the { predicate parameter foreach (T item in source) if (predicate(item)) yield return item; Implementation } } string[] names = { "Burke", "Connor", "Frank }; IEnumerable