C# 3.0 Language Innovations

Preview:

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

Citation preview

1

C# 3.0 Language InnovationsC# 3.0 Language Innovationsvar CommonWords =var CommonWords = from w in wordOccurancesfrom w in wordOccurances where w.Count > 2where w.Count > 2 select new { f.Name, w.Word, W.Count };select new { f.Name, w.Word, W.Count };

var CommonWords =var CommonWords = wordOccuranceswordOccurances .Where(w => w.Count > 2).Where(w => w.Count > 2) .Select(w => new {f.Name, w.Word, W.Count });.Select(w => new {f.Name, w.Word, W.Count });

Extension Extension methodsmethods

Lambda Lambda expressionsexpressions

Query Query expressionsexpressions

Object Object initializersinitializers

Anonymous Anonymous typestypes

Local Local variable type variable type inferenceinference

2

public delegate bool Predicate<T>(T obj);public delegate bool Predicate<T>(T obj);

public class List<T>public class List<T>{{ public List<T> FindAll(Predicate<T> test) public List<T> FindAll(Predicate<T> test) { … }{ … } … …}}

Lambda ExpressionsLambda Expressions

List<Customer> customers = List<Customer> customers = GetCustomerList();GetCustomerList();

List<Customer> x = customers.FindAll(List<Customer> x = customers.FindAll( delegate(Customer c) { return c.State == delegate(Customer c) { return c.State == "WA"; }"WA"; }););

List<Customer> x = customers.FindAll(List<Customer> x = customers.FindAll(c => c.State c => c.State == "WA"== "WA"););

ExplicitlyExplicitlytypedtyped

Statement Statement contextcontext

ImplicitlyImplicitlytypedtyped

Expression Expression contextcontext

3

Lambda ExpressionsLambda Expressionspublic delegate T Func<T>();public delegate T Func<T>();public delegate T Func<A0, T>(A0 arg0);public delegate T Func<A0, T>(A0 arg0);public delegate T Func<A0, A1, T>(A0 arg0, public delegate T Func<A0, A1, T>(A0 arg0, A1 arg1);A1 arg1);……Func<Customer, bool> test = Func<Customer, bool> test = c => c.State c => c.State == "WA"== "WA";;

Func<int, int, int> f = Func<int, int, int> f = (x, y) => x * y(x, y) => x * y;;

Func<int, int, int> comparer =Func<int, int, int> comparer = (int x, int y) => {(int x, int y) => { if (x > y) return 1;if (x > y) return 1; if (x < y) return -1;if (x < y) return -1; return 0;return 0; }};;

double factor = 2.0;double factor = 2.0;Func<double, double> f = Func<double, double> f = x => x * factorx => x * factor;;

4

Queries Through APIsQueries Through APIspublic class List<T>public class List<T>{{ public List<T> Where(Func<T, bool> predicate) { … }public List<T> Where(Func<T, bool> predicate) { … } public List<S> Select<S>(Func<T, S> selector) { … }public List<S> Select<S>(Func<T, S> selector) { … } … …}}

List<Customer> customers = List<Customer> customers = GetCustomerList();GetCustomerList();

List<string> contacts =List<string> contacts = customers.Where(c => c.State == "WA").Select(c customers.Where(c => c.State == "WA").Select(c => c.Name);=> c.Name);

Query operators Query operators are just are just methodsmethods

Type inference Type inference figures out <S>figures out <S>

But what about But what about other types?other types? Declare Declare

operators in all operators in all collections?collections?

What about What about arrays?arrays?

Methods Methods compose to compose to form queriesform queries

5

Queries Through APIsQueries Through APIspublic static class Sequencepublic static class Sequence{{ public static IEnumerable<T> Where<T>(IEnumerable<T> public static IEnumerable<T> Where<T>(IEnumerable<T> source,source, Func<T, bool> predicate) { … }Func<T, bool> predicate) { … }

public static IEnumerable<S> Select<T, public static IEnumerable<S> Select<T, S>(IEnumerable<T> source,S>(IEnumerable<T> source, Func<T, S> selector) { … }Func<T, S> selector) { … } … …}}

Customer[] customers = GetCustomerArray();Customer[] customers = GetCustomerArray();

IEnumerable<string> contacts = Sequence.Select(IEnumerable<string> contacts = Sequence.Select( Sequence.Where(customers, c => c.State == Sequence.Where(customers, c => c.State == "WA"),"WA"), c => c.Name);c => c.Name);

Query operators Query operators are static are static methodsmethods

Huh?Huh?

Want methods Want methods on on

IEnumerable<TIEnumerable<T>>

6

Extension MethodsExtension Methodsclass Customer { public string Name; }class Customer { public string Name; }

Customer[ ] c = new Customer[1];Customer[ ] c = new Customer[1];

c._c._

Huh ?!!?!Huh ?!!?!

7

namespace System.Querynamespace System.Query{{ public static class Sequencepublic static class Sequence {{ public static IEnumerable<T> Where<T>(this public static IEnumerable<T> Where<T>(this IEnumerable<T> source,IEnumerable<T> source, Func<T, bool> predicate) { … }Func<T, bool> predicate) { … }

public static IEnumerable<S> Select<T, S>(this public static IEnumerable<S> Select<T, S>(this IEnumerable<T> source,IEnumerable<T> source, Func<T, S> selector) { … }Func<T, S> selector) { … } … … }}}}

Extension MethodsExtension Methods

using System.Query;using System.Query;

ExtensionExtensionmethodsmethods

IEnumerable<string> contacts =IEnumerable<string> contacts = customers.Where(c => c.State == "WA").Select(c customers.Where(c => c.State == "WA").Select(c => c.Name);=> c.Name);

Brings Brings extensions into extensions into

scopescope

obj.Foo(x, y)obj.Foo(x, y)

XXX.Foo(obj, x, XXX.Foo(obj, x, y)y)

IntelliSense!IntelliSense!

8

Extension MethodsExtension Methods

public static class MyExtensions public static class MyExtensions {{ public static IEnumerable<T> Where<T>(public static IEnumerable<T> Where<T>( this IEnumerable<T> source,this IEnumerable<T> source, Func<T, bool> predicate) Func<T, bool> predicate) {{ foreach (T item in source)foreach (T item in source) if (predicate(item))if (predicate(item)) yield return item;yield return item; }} }}

9

Extension MethodsExtension Methods

public static class MyExtensionspublic static class MyExtensions{{ public static IEnumerable<T> public static IEnumerable<T> Where<T>Where<T>(( this IEnumerable<T> source,this IEnumerable<T> source, Func<T, bool> predicate) Func<T, bool> predicate) {{ foreach (T item in source)foreach (T item in source) if (predicate(item))if (predicate(item)) yield return item;yield return item; }} }}

Method is Method is named ‘Where’named ‘Where’

10

Extension MethodsExtension Methods

public static class MyExtensionspublic static class MyExtensions{{ public static IEnumerable<T> public static IEnumerable<T> Where<T>Where<T>(( this IEnumerable<T> sourcethis IEnumerable<T> source,, Func<T, bool> predicate) Func<T, bool> predicate) {{ foreach (T item in source)foreach (T item in source) if (predicate(item))if (predicate(item)) yield return item;yield return item; }} }}

Method is Method is named ‘Where’named ‘Where’

Method extends Method extends objects of typeobjects of typeIEnumerable<TIEnumerable<T

>>

11

Extension MethodsExtension Methods

public static class MyExtensionspublic static class MyExtensions{{ public static IEnumerable<T> public static IEnumerable<T> Where<T>Where<T>(( this IEnumerable<T> sourcethis IEnumerable<T> source,, Func<T, bool> predicate) Func<T, bool> predicate) {{ foreach (T item in source)foreach (T item in source) if (predicate(item))if (predicate(item)) yield return item;yield return item; }} }}

Method is Method is named ‘Where’named ‘Where’

Method extends Method extends objects of typeobjects of typeIEnumerable<TIEnumerable<T

>>

ImplementationImplementation

12

Extension MethodsExtension Methods

public static class MyExtensionspublic static class MyExtensions{{ public static IEnumerable<T> public static IEnumerable<T> Where<T>Where<T>(( this IEnumerable<T> sourcethis IEnumerable<T> source,, Func<T, bool> predicateFunc<T, bool> predicate) ) {{ foreach (T item in source)foreach (T item in source) if (predicate(item))if (predicate(item)) yield return item;yield return item; }} }}

Method is Method is named ‘Where’named ‘Where’

Method extends Method extends objects of typeobjects of typeIEnumerable<TIEnumerable<T

>>

Signature of the Signature of the predicate predicate parameterparameter

ImplementationImplementation

13

Extension MethodsExtension Methods

public static class MyExtensionspublic static class MyExtensions{{ public static IEnumerable<T> public static IEnumerable<T> Where<T>Where<T>(( this IEnumerable<T> sourcethis IEnumerable<T> source,, Func<T, bool> predicateFunc<T, bool> predicate) ) {{ foreach (T item in source)foreach (T item in source) if (predicate(item))if (predicate(item)) yield return item;yield return item; }} }}

Method is Method is named ‘Where’named ‘Where’

Method extends Method extends objects of typeobjects of typeIEnumerable<TIEnumerable<T

>>

Signature of the Signature of the predicate predicate parameterparameter

ImplementationImplementation

string[] names = { "Burke", "Connor", "Frank“ };string[] names = { "Burke", "Connor", "Frank“ };

IEnumerable<string> expr = IEnumerable<string> expr = MyExtensions.Where(names, MyExtensions.Where(names, s => s.Length < 6);s => s.Length < 6);

14

Extension MethodsExtension Methods

public static class MyExtensionspublic static class MyExtensions{{ public static IEnumerable<T> public static IEnumerable<T> Where<T>Where<T>(( this IEnumerable<T> sourcethis IEnumerable<T> source,, Func<T, bool> predicateFunc<T, bool> predicate) ) {{ foreach (T item in source)foreach (T item in source) if (predicate(item))if (predicate(item)) yield return item;yield return item; }} }}

Method is Method is named ‘Where’named ‘Where’

Method extends Method extends objects of typeobjects of typeIEnumerable<TIEnumerable<T

>>

Signature of the Signature of the predicate predicate parameterparameter

ImplementationImplementation

string[] names = { "Burke", "Connor", "Frank“ };string[] names = { "Burke", "Connor", "Frank“ };

IEnumerable<string> expr = IEnumerable<string> expr = namesnames.Where(names, .Where(names, s => s.Length < 6);s => s.Length < 6);

[System.Runtime.CompilerServices.Extension][System.Runtime.CompilerServices.Extension]

15

Anonymous TypesAnonymous Typespublic class Customerpublic class Customer{{ public string Name;public string Name; public Address Address;public Address Address; public string Phone;public string Phone; public List<Order> Orders;public List<Order> Orders; … …}}

public class Contactpublic class Contact{{ public string Name;public string Name; public string Phone;public string Phone;}}

Customer c = GetCustomer(…);Customer c = GetCustomer(…);Contact x = Contact x = new Contact { Name = c.Name, Phone = new Contact { Name = c.Name, Phone = c.Phone }c.Phone };;

Customer c = GetCustomer(…);Customer c = GetCustomer(…);var x = var x = new { c.Name, c.Phone }new { c.Name, c.Phone };;

Customer c = GetCustomer(…);Customer c = GetCustomer(…);var x = var x = new { Name = c.Name, Phone = c.Phone }new { Name = c.Name, Phone = c.Phone };;

class ???class ???{{ public string public string Name;Name; public string public string Phone;Phone;}}

Projection style Projection style initializerinitializer

16

var contacts =var contacts = from c in customersfrom c in customers where c.State == "WA"where c.State == "WA" select new { c.Name, c.Phone };select new { c.Name, c.Phone };

Anonymous TypesAnonymous Types

var contacts =var contacts = customers.customers. .Where(c => c.State == "WA“).Where(c => c.State == "WA“) .Select(c => new { c.Name, .Select(c => new { c.Name, c.Phone });c.Phone });

class ???class ???{{ public string public string Name;Name; public string public string Phone;Phone;}}

IEnumerable<???IEnumerable<???>>

foreach (var c in contacts) {foreach (var c in contacts) { Console.WriteLine(c.Name);Console.WriteLine(c.Name); Console.WriteLine(c.Phone);Console.WriteLine(c.Phone);}}

??????

17

Object InitializersObject Initializerspublic class Pointpublic class Point{{ private int x, y;private int x, y;

public int X { get { return x; } set { x = public int X { get { return x; } set { x = value; } }value; } } public int Y { get { return y; } set { y = public int Y { get { return y; } set { y = value; } }value; } }}}

Point a = new Point { X = 0, Y Point a = new Point { X = 0, Y = 1 };= 1 };

Point a = new Point();Point a = new Point();a.X = 0;a.X = 0;a.Y = 1;a.Y = 1;

Field or property Field or property assignmentsassignments

18

Object InitializersObject Initializerspublic class Rectanglepublic class Rectangle{{ private Point p1 = new Point();private Point p1 = new Point(); private Point p2 = new Point();private Point p2 = new Point();

public Point P1 { get { return p1; } }public Point P1 { get { return p1; } } public Point P2 { get { return p2; } }public Point P2 { get { return p2; } }}}

Rectangle r = new Rectangle {Rectangle r = new Rectangle { P1 = { X = 0, Y = 1 },P1 = { X = 0, Y = 1 }, P2 = { X = 2, Y = 3 }P2 = { X = 2, Y = 3 }};};

Rectangle r = new Rectangle r = new Rectangle();Rectangle();r.P1.X = 0;r.P1.X = 0;r.P1.Y = 1;r.P1.Y = 1;r.P2.X = 2;r.P2.X = 2;r.P2.Y = 3;r.P2.Y = 3;

Embedded Embedded objectsobjects

No “new No “new Point”Point”

Read-only Read-only propertiesproperties

19

Collection InitializersCollection Initializers

List<int> powers = new List<int> { 1, 10, 100, List<int> powers = new List<int> { 1, 10, 100, 1000, 10000 };1000, 10000 };

Must Must implement implement

ICollection<T>ICollection<T>

List<int> powers = new List<int> powers = new List<int>();List<int>();powers.Add(1);powers.Add(1);powers.Add(10);powers.Add(10);powers.Add(100);powers.Add(100);powers.Add(1000);powers.Add(1000);powers.Add(10000);powers.Add(10000);

20

Collection InitializersCollection Initializerspublic class Contactpublic class Contact{{ private string name;private string name; private List<string> phoneNumbers = new List<string>();private List<string> phoneNumbers = new List<string>();

public string Name { get { return name; } set { name = public string Name { get { return name; } set { name = value; } }value; } } public List<string> PhoneNumbers { get { return public List<string> PhoneNumbers { get { return phoneNumbers; } }phoneNumbers; } }}} List<Contact> contacts = new List<Contact> {List<Contact> contacts = new List<Contact> {

new Contact {new Contact { Name = "Chris Smith",Name = "Chris Smith", PhoneNumbers = { "206-555-0101", "425-882-PhoneNumbers = { "206-555-0101", "425-882-8080" }8080" } },}, new Contact {new Contact { Name = "Bob Harris",Name = "Bob Harris", PhoneNumbers = { "650-555-0199" }PhoneNumbers = { "650-555-0199" } }}};};

21

Local Variable Type Local Variable Type InferenceInference

int i = 5;int i = 5;string s = "Hello";string s = "Hello";double d = 1.0;double d = 1.0;int[] numbers = new int[] {1, 2, 3};int[] numbers = new int[] {1, 2, 3};Dictionary<int,Order> orders = new Dictionary<int,Order> orders = new Dictionary<int,Order>();Dictionary<int,Order>();

var i = 5;var i = 5;var s = "Hello";var s = "Hello";var d = 1.0;var d = 1.0;var numbers = new int[] {1, 2, 3};var numbers = new int[] {1, 2, 3};var orders = new Dictionary<int,Order>();var orders = new Dictionary<int,Order>();

““var” means var” means same type as same type as

initializerinitializer

22

Expression TreesExpression Treespublic class Northwind: DataContextpublic class Northwind: DataContext{{ public Table<Customer> public Table<Customer> Customers;Customers; public Table<Order> Orders;public Table<Order> Orders; … …}} Northwind db = new Northwind(…);Northwind db = new Northwind(…);

var query = from c in db.Customers where c.State == var query = from c in db.Customers where c.State == "WA" select c;"WA" select c;

Northwind db = new Northwind(…);Northwind db = new Northwind(…);var query = db.Customers.Where(c => c.State var query = db.Customers.Where(c => c.State == "WA");== "WA");

How does this How does this get remoted?get remoted?

public class Table<T>: IEnumerable<T>public class Table<T>: IEnumerable<T>{{ public Table<T> Where(Expression<Func<T, bool>> public Table<T> Where(Expression<Func<T, bool>> predicate);predicate); … …}}

Method asks Method asks for expression for expression

treetree

System.ExpressionSystem.Expressions.s.

Expression<T>Expression<T>

23

Expression TreesExpression TreesCode as DataCode as Data

Func<Customer, bool> test = c => c.State Func<Customer, bool> test = c => c.State == "WA";== "WA";

Expression<Func<Customer, bool>> test = c => c.State Expression<Func<Customer, bool>> test = c => c.State == "WA";== "WA";ParameterExpression c =ParameterExpression c = Expression.Parameter(typeof(Customer), "c");Expression.Parameter(typeof(Customer), "c");Expression expr =Expression expr = Expression.EQ(Expression.EQ( Expression.Property(c, Expression.Property(c, typeof(Customer).GetProperty("State")),typeof(Customer).GetProperty("State")), Expression.Constant("WA")Expression.Constant("WA") ););Expression<Func<Customer, bool>> test =Expression<Func<Customer, bool>> test = Expression.Lambda<Func<Customer, bool>>(expr, c);Expression.Lambda<Func<Customer, bool>>(expr, c);

24

Query ExpressionsQuery ExpressionsLanguage integrated query syntaxLanguage integrated query syntax

fromfrom idid inin sourcesource{ { fromfrom idid inin sourcesource | | wherewhere conditioncondition } }[ [ orderbyorderby orderingordering, , orderingordering, , …… ] ]selectselect exprexpr | | groupgroup exprexpr byby keykey[ [ intointo idid queryquery ] ]

Starts with Starts with fromfrom Zero or more Zero or more

fromfrom or or wherewhere

Optional Optional orderbyorderby

Ends with Ends with selectselect or or groupgroup bybyOptional Optional intointo

continuationcontinuation

25

from c in customersfrom c in customerswhere c.State == "WA"where c.State == "WA"select new { c.Name, c.Phone };select new { c.Name, c.Phone };

customerscustomers.Where(c => c.State == "WA").Where(c => c.State == "WA").Select(c => new { c.Name, c.Phone });.Select(c => new { c.Name, c.Phone });

Query ExpressionsQuery ExpressionsQueries translate to method Queries translate to method invocationsinvocations

Where, Select, SelectMany, OrderBy, Where, Select, SelectMany, OrderBy, GroupByGroupBy

26

C# 3.0 Language C# 3.0 Language InnovationsInnovations

Lambda expressionsLambda expressionsExtension methodsExtension methodsLocal variable type inferenceLocal variable type inferenceObject initializersObject initializersAnonymous typesAnonymous typesQuery expressionsQuery expressionsExpression treesExpression trees

var x = 5;var x = 5;

static void Dump(this static void Dump(this object o);object o);

c => c => c.Namec.Name

new Point { x = 1, y new Point { x = 1, y = 2 }= 2 }

new { c.Name, new { c.Name, c.Phone }c.Phone }

from … where … from … where … selectselect

Expression<TExpression<T>>

Recommended