Presentation is loading. Please wait.

Presentation is loading. Please wait.

2 C# 1.0 C# 2.0 C# 3.0 Komponensek felügyelt környezetben • Property, delegate, event Típusbiztosabb, hatékonyabb nyelv • Generikus típusok, yield return.

Similar presentations


Presentation on theme: "2 C# 1.0 C# 2.0 C# 3.0 Komponensek felügyelt környezetben • Property, delegate, event Típusbiztosabb, hatékonyabb nyelv • Generikus típusok, yield return."— Presentation transcript:

1

2 2

3 C# 1.0 C# 2.0 C# 3.0 Komponensek felügyelt környezetben • Property, delegate, event Típusbiztosabb, hatékonyabb nyelv • Generikus típusok, yield return Nyelvbe ágyazott lekérdezések

4 public class List { T[] array = new T[10]; // tároló public void Add( T item ) { this.array[count++] = item; } public class List { T[] array = new T[10]; // tároló public void Add( T item ) { this.array[count++] = item; }

5 public IEnumerable Filter() { foreach( T t in this ) if( t.ToString().StartsWith( "S" ) ) yield return t; }... static void Main() {... foreach( int i in list.Filter() ) Console.WriteLine( i ); } public IEnumerable Filter() { foreach( T t in this ) if( t.ToString().StartsWith( "S" ) ) yield return t; }... static void Main() {... foreach( int i in list.Filter() ) Console.WriteLine( i ); }

6 public IEnumerable Filter( Predicate filter ) { foreach( T t in this ) if( filter( t ) ) yield return t; }... static void Main() {... foreach( int i in list.Filter( delegate( string s ) { return s.StartsWith( "S" ); } ) ) Console.WriteLine( i ); } public IEnumerable Filter( Predicate filter ) { foreach( T t in this ) if( filter( t ) ) yield return t; }... static void Main() {... foreach( int i in list.Filter( delegate( string s ) { return s.StartsWith( "S" ); } ) ) Console.WriteLine( i ); }

7 7

8 delegate( string s ) { return s.StartsWith( "S" ); } s => s.StartsWith( "S" ) ( x, y ) => Math.Sqrt( x * x + y * y ) () => 42

9 public class List { public int Count { get; private set; } public void Add( T item ) { this.array[Count++] = item; } public class List { public int Count { get; private set; } public void Add( T item ) { this.array[Count++] = item; } private int <>field0; public int Count { get { return <>field0; } private set { <>field0 = value; } } private int <>field0; public int Count { get { return <>field0; } private set { <>field0 = value; } }

10 namespace linqSample.Utils { public static class Helper { static public IEnumerable Filter( this IEnumerable seq, Predicate filter ) {... } namespace linqSample.Utils { public static class Helper { static public IEnumerable Filter( this IEnumerable seq, Predicate filter ) {... } Statikus bővítő metódus A bővítendő típus a ‘this’ kulcsszó után A névtérben lévő összes bővítőmetódus engedélyezése using linqSample.Utils;

11 List list = new List (); foreach( string x in list.Filter( s => s.StartsWith( "S" ) ) ) List list = new List (); foreach( string x in list.Filter( s => s.StartsWith( "S" ) ) ) var list = new List (); foreach( var x in list.Filter( s => s.StartsWith( "S" ) ) ) var list = new List (); foreach( var x in list.Filter( s => s.StartsWith( "S" ) ) )

12 12

13 var b=new Music{ Title="Soul Bop",Length=591 } new Music { Title = "Soul Bop", Length = 591 }. Dump(); list.Add( new Music { Title = "Soul Bop", Length = 591 } ); var b0=new Music(); b0.Title="Soul Bop"; b0.Length=591; var b = b0; var b0=new Music(); b0.Title="Soul Bop"; b0.Length=591; var b = b0;

14 new Music { Title = "Soul Bop", Length = 591, Album = { Artist = "Bill Evans", Year = 2000 } } new Music { Title = "Soul Bop", Length = 591, Album = { Artist = "Bill Evans", Year = 2000 } } new Music { Title = "Soul Bop", Length = 591, Album = new Album{ Artist = "Bill Evans", Year = 2000 } } new Music { Title = "Soul Bop", Length = 591, Album = new Album{ Artist = "Bill Evans", Year = 2000 } }

15 var szamok = new Dictionary { { 0, "nulla" }, { 1, "egy" }, { 2, "kettő" } }; var szamok = new Dictionary { { 0, "nulla" }, { 1, "egy" }, { 2, "kettő" } }; var v = new [ ] { 1, 1.5, 2.3 }; // double [ ] var list = new List { new Music { Title = "Soul Bop", Length = 591 }, new Music { Title = "Hangin' in the City", Length = 397 } }; var list = new List { new Music { Title = "Soul Bop", Length = 591 }, new Music { Title = "Hangin' in the City", Length = 397 } };

16 var list = new [ ] { new { Title = "Soul Bop", Length = 591 }, new { Title = "Hangin' in the City", Length = 397 } }; foreach( var x in list.Filter( s => s.Length < 300 ) ) Console.WriteLine( x ); var list = new [ ] { new { Title = "Soul Bop", Length = 591 }, new { Title = "Hangin' in the City", Length = 397 } }; foreach( var x in list.Filter( s => s.Length < 300 ) ) Console.WriteLine( x );

17 17

18

19 SzűrésWhere ProjekcióSelect, SelectMany RendezésOrderBy, ThenBy CsoportosításGroupBy JoinokJoin, GroupJoin QuantifiersAny, All ParticionálásTake, Skip, TakeWhile, SkipWhile HalmazműveletekDistinct, Union, Intersect, Except ElemekFirst, Last, Single, ElementAt AggregációCount, Sum, Min, Max, Average KonverzióToArray, ToList, ToDictionary KasztolásOfType, Cast

20 20

21 from m in list where m.Title.StartsWith( "S" ) select m. Title; from m in list where m.Title.StartsWith( "S" ) select m. Title; list.Where( m => m. Title.StartsWith( "S" ) ).Select( m => m.Title); list.Where( m => m. Title.StartsWith( "S" ) ).Select( m => m.Title);

22 from id in source { from id in source | join id in source on expr equals expr [ into id ] | let id = expr | where condition | orderby ordering, ordering, … } select expr | group expr by key [ into id query ] from-mal kezdődik További from, join, let, where, vagy orderby Végül select by group by Opcionálisan into-val folytatható A Query Expression Pattern metódusai Where Select, SelectMany OrderBy, OrderByDescending, ThenBy, ThenByDescending GroupBy Join, GroupJoin Cast

23 list TitleLengthID select m => m.Title cimek m => m.Title.StartsWith("S”) where var list = new List {... }; q.ToList(); var q = from m in list where m.Title.StartsWith( "S" ) select m.Title; var q = list.Where( m => m.Title.StartsWith( "S" ) ). Select( m => m.Title);

24 24

25 from m in list where m.Title.StartsWith( "S" ) select m.Title; SELECT Title FROM Musicok WHERE Title LIKE ‘S%’

26 Expression > expr = m => m.Title.StartsWith( "S" ); public delegate bool Predicate ( T obj ); ParameterExpression p= Expression.Parameter(typeof(Music),"m"); expr=Expression.Lambda >( Expression.Call( Expression.Property(p, GetMethod(Music.get_Title)), GetMethod(string.StartsWith), new Expression [ ] {Expression.Constant("S", typeof(string)) }), new ParameterExpression [ ] { p }); ParameterExpression p= Expression.Parameter(typeof(Music),"m"); expr=Expression.Lambda >( Expression.Call( Expression.Property(p, GetMethod(Music.get_Title)), GetMethod(string.StartsWith), new Expression [ ] {Expression.Constant("S", typeof(string)) }), new ParameterExpression [ ] { p });

27 27 public static IQueryable Where ( this IQueryable source, Expression > predicate); public static IQueryable Where ( this IQueryable source, Expression > predicate); list. Where(…). Take(…). Select (…); // végül egy Expression lesz

28 28 SELECT System.Title, System.Media.Duration FROM SystemIndex WHERE System.Title = 'Soul Bop'

29 29 new WDS(). Where( m => m.Title.StartsWith( "S" ) && m.Length > 300 ); MethodCall Where Object (root) WDS WDSLambdaExp m => … LambdaExp GreaterThan … > … GreaterThan MemberAccessm.LengthMemberAccessm.LengthConstant300Constant300 MethodCall ….StartsWith( … ) MethodCall MemberAccessm.TitleMemberAccessm.TitleConstant“S”Constant“S” AndAlso … && … AndAlso

30 System.Linq.Enumerable delegate alapú A forrás IEnumerable System.Linq.Queryable kifejezésfa alapú A forrás IQueryable SQL DataSet Objektumok Egyéb... from m in list where m.Title.StartsWith( "S" ) select m.Title; var q = list.Where( m => m.Title.StartsWith( "S" ) ). Select( m => m.Title);

31 31

32 var q = from m in list where m.Title.StartsWith( "S" ) select new { m.Title, m.Length }; var q = from m in list where m.Title.StartsWith( "S" ) select new { m.Title, m.Length }; var q = list.Where( m => m.Length.StartsWith( "S" ) ).Select( m => new { m.Title, m.Length } ); var q = list.Where( m => m.Length.StartsWith( "S" ) ).Select( m => new { m.Title, m.Length } ); Bővítő metódusok Lambda kifejezések Lekérdezések Objektum inicializálók Névtelen típusok Implicit típusú lokális változó

33

34 Objektumok XML.NET Language Integrated Query C# 3.0 VB 9.0 más nyelvek… Relációs adatbázisok LINQ to Objects LINQ to SQL LINQ to XML LINQ to Entities LINQ to DataSets

35

36 36

37 37

38 38

39 Func Sort = xs => xs.Case( () => xs, ( head,tail ) => ( Sort( tail.Where( x => x < head ) ) ).Concat ( Single( head ) ).Concat ( Sort( tail.Where( x => x >= head ) ) ) ); type inference összefűzés magasabb rendű funkció típussal paraméterezett funkció rekurzió szűrés lambda kifejezés

40 © 2007 Microsoft Corporation. Minden jog fenntartva. Az előadás csupán tájékoztató jellegű, a Microsoft Magyarország a benne szereplő esetleges hibákért vagy tévedésekért semmilyen felelősséget nem vállal.


Download ppt "2 C# 1.0 C# 2.0 C# 3.0 Komponensek felügyelt környezetben • Property, delegate, event Típusbiztosabb, hatékonyabb nyelv • Generikus típusok, yield return."

Similar presentations


Ads by Google