Prezentace se nahrává, počkejte prosím

Prezentace se nahrává, počkejte prosím

Kam se ubírá jazyk C#? Dr. Ing. Dalibor Kačmář Academic Developer Evangelist Developer & Platform Evangelist Group Microsoft Czech & Slovakia Anders Hejlsberg.

Podobné prezentace


Prezentace na téma: "Kam se ubírá jazyk C#? Dr. Ing. Dalibor Kačmář Academic Developer Evangelist Developer & Platform Evangelist Group Microsoft Czech & Slovakia Anders Hejlsberg."— Transkript prezentace:

1 Kam se ubírá jazyk C#? Dr. Ing. Dalibor Kačmář Academic Developer Evangelist Developer & Platform Evangelist Group Microsoft Czech & Slovakia Anders Hejlsberg Technical Fellow Microsoft Corporation

2 Projekt LINQ Standardnídotazovacíoperátory Objekty DLinq (ADO.NET) XLinq(System.Xml) XML.NET Language Integrated Query C#VB Další… SQLWinFS

3 C# 3.0 – Cíle návrhu Integrované objekty, relační a XML Postaveno za základech položených v C# 1.0 a 2.0 Běží na.NET 2.0 (“Whidbey”) CLR Nesvazuje jazyk se specifickými APIs Zůstává 100% zpětně kompatibilní

4 4 Language Integrated Query

5 5 Inovace v jazyce C# 3.0 var contacts = from c in customers from c in customers where c.State == "WA" where c.State == "WA" select new { c.Name, c.Phone }; select new { c.Name, c.Phone }; var contacts = customers customers.Where(c => c.State == "WA").Where(c => c.State == "WA").Select(c => new { c.Name, c.Phone });.Select(c => new { c.Name, c.Phone }); Extension metody Lambda expressions Query výraz Inicializátory objektu Anonymnítypy Dedukce typu lokání proměnné

6 6 public delegate bool Predicate (T obj); public class List public class List { public List FindAll(Predicate test) { … } public List FindAll(Predicate test) { … } …} Lambda výrazy List customers = GetCustomerList(); List x = customers.FindAll( delegate(Customer c) { return c.State == "WA"; } delegate(Customer c) { return c.State == "WA"; }); List x = customers.FindAll(c => c.State == "WA"); Explicitně typový Statement context Implicitnětypový Expression context

7 7 Lambda výrazy public delegate T Func (); public delegate T Func (A0 arg0); public delegate T Func (A0 arg0, A1 arg1); … Func test = c => c.State == "WA"; Func f = (x, y) => x * y; Func 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; Func f = x => x * factor;

8 8 Dotazy přes APIs public class List public class List { public List Where(Func predicate) { … } public List Where(Func predicate) { … } public List Select (Func selector) { … } public List Select (Func selector) { … } …} List customers = GetCustomerList(); List contacts = customers.Where(c => c.State == "WA").Select(c => c.Name); customers.Where(c => c.State == "WA").Select(c => c.Name); Dotazovací operátory jsou prosté metody Typová dedukce zjistí Typová dedukce zjistí Ale co ostatní typy? Deklarovat operátory ve všech kolekcích? A co pole? Metody se skládají do formy dotazu

9 9 Dotazy přes APIs public static class Sequence { public static IEnumerable Where (IEnumerable source, public static IEnumerable Where (IEnumerable source, Func predicate) { … } Func predicate) { … } public static IEnumerable Select (IEnumerable source, public static IEnumerable Select (IEnumerable source, Func selector) { … } Func selector) { … } …} Customer[] customers = GetCustomerArray(); IEnumerable contacts = Sequence.Select( Sequence.Where(customers, c => c.State == "WA"), Sequence.Where(customers, c => c.State == "WA"), c => c.Name); c => c.Name); Dotazovací operátory jsou statické metody Huh? Chci metodu na IEnumerable Chci metodu na IEnumerable

10 10 namespace System.Query { public static class Sequence public static class Sequence { public static IEnumerable Where (this IEnumerable source, public static IEnumerable Where (this IEnumerable source, Func predicate) { … } Func predicate) { … } public static IEnumerable Select (this IEnumerable source, public static IEnumerable Select (this IEnumerable source, Func selector) { … } Func selector) { … } … }} Extension metody using System.Query; Extension metody IEnumerable contacts = customers.Where(c => c.State == "WA").Select(c => c.Name); customers.Where(c => c.State == "WA").Select(c => c.Name); Přinese extensions do rozsahu obj.Foo(x, y)  XXX.Foo(obj, x, y) IntelliSense!

11 11 Lambda výrazy a Extension metody

12 12 Inicializátory objektů public class Point { private int x, y; private int x, y; public int X { get { return x; } set { x = value; } } public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = 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; Přiřazení položky nebo vlastnosti

13 13 Inicializátory objektů public 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 { 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.P1.X = 0; r.P1.Y = 1; r.P2.X = 2; r.P2.Y = 3; Vložené objekty Ne “new Point” Read-only vlastnosti

14 14 Inicializátory kolekcí List powers = new List { 1, 10, 100, 1000, }; Musí implementovat ICollection Musí implementovat ICollection List powers = new List (); powers.Add(1);powers.Add(10);powers.Add(100);powers.Add(1000);powers.Add(10000);

15 15 Inicializátory kolekcí public class Contact { private string name; private string name; private List phoneNumbers = new List (); private List phoneNumbers = new List (); public string Name { get { return name; } set { name = value; } } public string Name { get { return name; } set { name = value; } } public List PhoneNumbers { get { return phoneNumbers; } } public List PhoneNumbers { get { return phoneNumbers; } }} List contacts = new List { new Contact { new Contact { Name = "Chris Smith", Name = "Chris Smith", PhoneNumbers = { " ", " " } PhoneNumbers = { " ", " " } }, }, new Contact { new Contact { Name = "Bob Harris", Name = "Bob Harris", PhoneNumbers = { " " } PhoneNumbers = { " " } }};

16 16 Dedukce typu lokální proměnné 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 (); “var” znamená stejný typ jako inicializátor

17 17 Anonymní typy public class Customer { public string Name; public string Name; public Address Address; public Address Address; public string Phone; public string Phone; public List Orders; public List Orders; …} public class Contact { public string Name; public string Name; public string Phone; public string Phone;} Customer c = GetCustomer(…); Contact x = new Contact { Name = c.Name, Phone = c.Phone }; Customer c = GetCustomer(…); var x = new { c.Name, c.Phone }; Customer c = GetCustomer(…); var x = new { Name = c.Name, Phone = c.Phone }; class ??? { public string Name; public string Name; public string Phone; public string Phone;} Projection style initializer

18 18 var contacts = from c in customers from c in customers where c.State == "WA" where c.State == "WA" select new { c.Name, c.Phone }; select new { c.Name, c.Phone }; Anonymní typy var contacts = customers. customers..Where(c => c.State == "WA“).Where(c => c.State == "WA“).Select(c => new { c.Name, c.Phone });.Select(c => new { c.Name, c.Phone }); class ??? { public string Name; public string Name; public string Phone; public string Phone;} IEnumerable foreach (var c in contacts) { Console.WriteLine(c.Name); Console.WriteLine(c.Name); Console.WriteLine(c.Phone); Console.WriteLine(c.Phone);} ???

19 19 Dotazovací výrazy Dotazovací syntaxe integrována do jazyka from id in source { from id in source | where condition } [ orderby ordering, ordering, … ] select expr | group expr by key [ into id query ] Začíná pomocí from Nula nebo více from nebo where Volitelně orderby Končí pomocí select nebo group by Volitelně into pokračování

20 20 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 }); Dotazovací výrazy Dotazy se překládají do volání metod Where, Select, SelectMany, OrderBy, GroupBy

21 21 Výrazové stromy public class Northwind: DataContext { public Table Customers; public Table Customers; public Table Orders; public Table Orders; …} Northwind db = new Northwind(…); var query = from c in db.Customers where c.State == "WA" select c; Northwind db = new Northwind(…); var query = db.Customers.Where(c => c.State == "WA"); Jak se to však spustí? public class Table : IEnumerable public class Table : IEnumerable { public Table Where(Expression > predicate); public Table Where(Expression > predicate); …} Metoda potřebuje výrazový strom System.Expressions. Expression System.Expressions. Expression

22 22 Výrazové stromy Kód jako Data Func test = c => c.State == "WA"; Expression > test = c => c.State == "WA"; ParameterExpression c = Expression.Parameter(typeof(Customer), "c"); Expression.Parameter(typeof(Customer), "c"); Expression expr = Expression.EQ( Expression.EQ( Expression.Property(c, typeof(Customer).GetProperty("State")), Expression.Property(c, typeof(Customer).GetProperty("State")), Expression.Constant("WA") Expression.Constant("WA") ); ); Expression > test = Expression.Lambda >(expr, c); Expression.Lambda >(expr, c);

23 23 LINQ Tech Preview

24 24 Inovace v jazyce C# 3.0 Lambda Výrazy Extension metody Dedukce typu lokání proměnné Inicializárory objektů Anonymní typy Dotazovací výrazy Výrazové stromy var x = 5; static void Dump(this object o); c => c.Name new Point { x = 1, y = 2 } new { c.Name, c.Phone } from … where … select Expression

25 25 Více informací

26 © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.


Stáhnout ppt "Kam se ubírá jazyk C#? Dr. Ing. Dalibor Kačmář Academic Developer Evangelist Developer & Platform Evangelist Group Microsoft Czech & Slovakia Anders Hejlsberg."

Podobné prezentace


Reklamy Google