1 C# 3.0 Language C# 3.0 Language Innovations Innovations var CommonWords = var CommonWords = from w in wordOccurances from w in wordOccurances where w.Count > 2 where w.Count > 2 select new { f.Name, w.Word, W.Count }; select new { f.Name, w.Word, W.Count }; var CommonWords = var CommonWords = wordOccurances wordOccurances .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 Extensio Extensio n n methods methods Lambda Lambda expression expression s Query Query expressions expressions Object Object initialize initialize rs rs Anonymous Anonymous types types Local Local variable variable type type inference inference
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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) { … }{ … } … …}}
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;;
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
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
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;}}
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;
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> {
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);