c# 3.0 ppt
TRANSCRIPT
-
8/12/2019 C# 3.0 PPT
1/96
C sharp 3.0
-
8/12/2019 C# 3.0 PPT
2/96
Implicit variable declaration
Anonymous Types
Extension Methods
Automatic Properties
Object Initializers
Collection Initializers
ambda Expressions !uery Expressions
Expression Trees
I"! to Collections
I"! to #ata$et
I"! to $!
Partial Methods
I"! to %M
-
8/12/2019 C# 3.0 PPT
3/96
Implicit variable declaration
-
8/12/2019 C# 3.0 PPT
4/96
-
8/12/2019 C# 3.0 PPT
5/96
Implicit declaration
-
8/12/2019 C# 3.0 PPT
6/96
Only &ithin method can declare this"ot even &ith inter'aces also
-
8/12/2019 C# 3.0 PPT
7/96
"ot even as parameter also
-
8/12/2019 C# 3.0 PPT
8/96
Ta(e home o' Implicit )ariable
*or local stac( based variables
Its nothin+ li(e late bindin+ or anythin+ o' that (ind,
Compiler is in'ers the type,
$tron+ly associate- lin(ed or used &ith Anonymous type
-
8/12/2019 C# 3.0 PPT
9/96
Anonymous Types
-
8/12/2019 C# 3.0 PPT
10/96
Anonymous Types
Anonymous Types allo& you to create a class structure
on the 'ly,
It uses Implicit )ariables
-
8/12/2019 C# 3.0 PPT
11/96
-
8/12/2019 C# 3.0 PPT
12/96
-
8/12/2019 C# 3.0 PPT
13/96
-
8/12/2019 C# 3.0 PPT
14/96
-
8/12/2019 C# 3.0 PPT
15/96
-
8/12/2019 C# 3.0 PPT
16/96
Creatin+ arrays o' Anonymous Types
-
8/12/2019 C# 3.0 PPT
17/96
-
8/12/2019 C# 3.0 PPT
18/96
-
8/12/2019 C# 3.0 PPT
19/96
-
8/12/2019 C# 3.0 PPT
20/96
Anonymous type +els and &or(s &ell
&ith the ambda Expressions
-
8/12/2019 C# 3.0 PPT
21/96
Extension Methods
-
8/12/2019 C# 3.0 PPT
22/96
Extension Methods
-
8/12/2019 C# 3.0 PPT
23/96
Extension Methods
Extension methods allo& developers to add ne& methods to thepublic contract o' an existin+ C. type- &ithout havin+ to sub/class
it or recompile the ori+inal type,
Extension methods are a ne& 'eature that allo&s you to enhance an
existin+ class by addin+ a ne& method to it &ithout modi'yin+ the
actual code 'or the class, This is especially use'ul &hen usin+ I"!
because several extension methods are available in &ritin+ I"!0uery expressions,
-
8/12/2019 C# 3.0 PPT
24/96
-
8/12/2019 C# 3.0 PPT
25/96
"ote ho& the static method above has a 1this1 (ey&ord be'ore the 'irst parameter
ar+ument o' type strin+, This tells the compiler that this particular Extension Method
should be added to objects o' type 2int1,
Only public ones +ets extended
-
8/12/2019 C# 3.0 PPT
26/96
-
8/12/2019 C# 3.0 PPT
27/96
-
8/12/2019 C# 3.0 PPT
28/96
-
8/12/2019 C# 3.0 PPT
29/96
Automatic Property
-
8/12/2019 C# 3.0 PPT
30/96
-
8/12/2019 C# 3.0 PPT
31/96
-
8/12/2019 C# 3.0 PPT
32/96
-
8/12/2019 C# 3.0 PPT
33/96
-
8/12/2019 C# 3.0 PPT
34/96
Note about that we aren't actually adding any logic in the getters/setters of
our properties instead we just get/set the value directly to a field.
-
8/12/2019 C# 3.0 PPT
35/96
-
8/12/2019 C# 3.0 PPT
36/96
-
8/12/2019 C# 3.0 PPT
37/96
Automatic Properties
-
8/12/2019 C# 3.0 PPT
38/96
3hen the C4 1Orcas1 compiler encounters an empty +et5setproperty implementation li(e above- it &ill no& automatically+enerate a private 'ield 'or you &ithin your class- and
implement a public +etter and setter property implementationto it,
3ith the C4 and )6 1Orcas1 compilers you can no& ta(eadvanta+e o' a +reat 1syntactic su+ar1 lan+ua+e 'eature called1object Initializers1
-
8/12/2019 C# 3.0 PPT
39/96
object Initializers
-
8/12/2019 C# 3.0 PPT
40/96
Object Initialization &ith Complex
Type
-
8/12/2019 C# 3.0 PPT
41/96
-
8/12/2019 C# 3.0 PPT
42/96
-
8/12/2019 C# 3.0 PPT
43/96
-
8/12/2019 C# 3.0 PPT
44/96
Collection Initializers
-
8/12/2019 C# 3.0 PPT
45/96
Collection Initializer
Initial the collection in a sin+le line o' code,
Collection need to implement Icollection7T8
-
8/12/2019 C# 3.0 PPT
46/96
As +ood as doin+ obj,add9: in the bac(+round
-
8/12/2019 C# 3.0 PPT
47/96
!uery Expression
-
8/12/2019 C# 3.0 PPT
48/96
I"!an+ua+e/Inte+rated !uery
9I"!:
-
8/12/2019 C# 3.0 PPT
49/96
I"! an+ua+e
an+ua+e/Inte+rated !uery 9I"!: is a +roundbrea(in+innovation in )isual $tudio ;
-
8/12/2019 C# 3.0 PPT
50/96
Traditionally
Traditionally- 0ueries a+ainst data are expressed assimple strin+s &ithout type chec(in+ at compile time orIntelli$ense support,
*urthermore- you have to learn a di''erent 0uery
lan+ua+e 'or each type o' data source> $! databases-%M documents- various 3eb services- and so on,
I"! ma(es a 0uery a 'irst/class lan+ua+e construct in
C4 and )isual 6asic, ?ou &rite 0ueries a+ainst stron+lytyped collections o' objects by usin+ lan+ua+e (ey&ordsand 'amiliar operators,
-
8/12/2019 C# 3.0 PPT
51/96
The following illustration shows apartially-completed LINQ queryagainst a SQL Server database in Cwith full type chec!ing and
IntelliSense support"
-
8/12/2019 C# 3.0 PPT
52/96
I"!
In )isual $tudio you can &rite I"! 0ueries in )isual6asic or C4 &ith $! $erver databases- %M
documents- A#O,"ET #atasets- and any collection o'
objects that supports IEnumerable or the +eneric
IEnumerable79O' 79T8:8:inter'ace,
I"! support 'or the A#O,"ET Entity *rame&or( is also
planned,
I"! providers are bein+ &ritten by third parties 'or
many 3eb services and other database
implementations,
http://msdn2.microsoft.com/en-us/library/h1x9x1b1.aspxhttp://msdn2.microsoft.com/en-us/library/9eekhta0.aspxhttp://msdn2.microsoft.com/en-us/library/9eekhta0.aspxhttp://msdn2.microsoft.com/en-us/library/h1x9x1b1.aspx -
8/12/2019 C# 3.0 PPT
53/96
Introduction to I"! !ueries
A 0uery is an expression that retrieves data 'rom a datasource, !ueries are usually expressed in a specialized0uery lan+ua+e, 9example $! 'or relational databasesand %!uery 'or %M, :
There'ore- developers have had to learn a ne& 0uerylan+ua+e 'or each type o' data source or data 'ormat thatthey must support,
"#N$ si!plifies this situation by offering aconsistent !odel for wor%ing with data acrossvarious %inds of data sources and for!ats.
-
8/12/2019 C# 3.0 PPT
54/96
Ta(e @ome
In a I"! 0uery- you are al&ays &or(in+ &ith objects,
?ou use the same basic codin+ patterns to 0uery and
trans'orm data in %M documents- $! databases-
A#O,"ET #atasets- ,"ET collections- and any other'ormat 'or &hich a I"! provider is available,
-
8/12/2019 C# 3.0 PPT
55/96
Three Parts o' a !uery Operation
All I"! 0uery operations consist o' three distinct actions>
Obtain the data source,
Create the 0uery, Execute the 0uery,
-
8/12/2019 C# 3.0 PPT
56/96
-
8/12/2019 C# 3.0 PPT
57/96
-
8/12/2019 C# 3.0 PPT
58/96
-
8/12/2019 C# 3.0 PPT
59/96
T ( h ;
-
8/12/2019 C# 3.0 PPT
60/96
Ta(e home ;
The 'rom clause speci'ies the data source- the &here
clause applies the 'ilter- and the select clause speci'ies
the type o' the returned elements,
The 0uery variable itsel' only stores the 0uery
commands, The actual execution o' the 0uery is de'erred
until you iterate over the 0uery variable in a foreach
statement, This concept is re'erred to as deferred
e&ecution.
i # di t ( ti
-
8/12/2019 C# 3.0 PPT
61/96
orcing #!!ediate (&ecution
!ueries that per'orm a++re+ation 'unctions over a ran+e
o' source elements must 'irst iterate over those
elements, Examples o' such 0ueries are Count- )a&-
*verage- and irst, These execute &ithout an explicit'oreach statement because the 0uery itsel' must use
'oreach in order to return a result, "ote also that these
types o' 0ueries return a sin+le value- not an
#(nu!erablecollection,
-
8/12/2019 C# 3.0 PPT
62/96
i # di t ( ti
-
8/12/2019 C# 3.0 PPT
63/96
orcing #!!ediate (&ecution
!ueries that per'orm a++re+ation 'unctions over a ran+e
o' source elements must 'irst iterate over those
elements, Examples o' such 0ueries are Count- )a&-
*verage- and irst, These execute &ithout an explicit'oreach statement because the 0uery itsel' must use
'oreach in order to return a result, "ote also that these
types o' 0ueries return a sin+le value- not an
#(nu!erablecollection,
-
8/12/2019 C# 3.0 PPT
64/96
-
8/12/2019 C# 3.0 PPT
65/96
-
8/12/2019 C# 3.0 PPT
66/96
The Collection Initializer
Expression !uery Anonymous
Type Implicit variable AutomaticProperty
-
8/12/2019 C# 3.0 PPT
67/96
Automatic Property
-
8/12/2019 C# 3.0 PPT
68/96
Collection initializer
-
8/12/2019 C# 3.0 PPT
69/96
!uery expression
-
8/12/2019 C# 3.0 PPT
70/96
#!plicitly +yped ,ariable.
*nony!ous +ypes
-
8/12/2019 C# 3.0 PPT
71/96
-
8/12/2019 C# 3.0 PPT
72/96
$ummary
*s developers we now have a !uch
!ore concise way to define objects-initialie the!- and add the! to
collections
-
8/12/2019 C# 3.0 PPT
73/96
ambda Expression
C 3 0 "anguage (nhance!ents for "#N$
-
8/12/2019 C# 3.0 PPT
74/96
C 3.0 "anguage (nhance!ents for "#N$
To truly understand much o' the syntax o' I"!- it is
necessary 'or me to cover some o' the ne& C4 B,
-
8/12/2019 C# 3.0 PPT
75/96
ambda Expressions
In C4 B,
-
8/12/2019 C# 3.0 PPT
76/96
-
8/12/2019 C# 3.0 PPT
77/96
-
8/12/2019 C# 3.0 PPT
78/96
-
8/12/2019 C# 3.0 PPT
79/96
This method should have na
-
8/12/2019 C# 3.0 PPT
80/96
-
8/12/2019 C# 3.0 PPT
81/96
-
8/12/2019 C# 3.0 PPT
82/96
-
8/12/2019 C# 3.0 PPT
83/96
-
8/12/2019 C# 3.0 PPT
84/96
-
8/12/2019 C# 3.0 PPT
85/96
-
8/12/2019 C# 3.0 PPT
86/96
sin+ ambda Expressions to Create
Expression Trees
-
8/12/2019 C# 3.0 PPT
87/96
I"! treats data as objects,
"#N$ provides the ability to treat e&pressions as data at
runti!e.
"#N$ de'ines a ne& type- (&pression+1- &hich represents an
expression tree- an in/memory representation o' a lambdaexpression,
Expression trees allo& developers to treat lambda expressions as
data- allo&in+ 'or inspection and modi'ication o' lambda expressionsin code,
-
8/12/2019 C# 3.0 PPT
88/96
"#N$ provides the ability to treat expressions as data at runtime, "#N$de'ines a ne& type- (&pression+1- &hich represents an expression tree-
an in memory representation o' a lambda expression
-
8/12/2019 C# 3.0 PPT
89/96
an in/memory representation o' a lambda expression,
-
8/12/2019 C# 3.0 PPT
90/96
-
8/12/2019 C# 3.0 PPT
91/96
Actually &e converted an expression tree into a dele+ate that can be executed
-
8/12/2019 C# 3.0 PPT
92/96
It is very easy to create and build expressions that can be compiled
dynamically and create ne& uncobjects that pass around code as objects
-
8/12/2019 C# 3.0 PPT
93/96
Conclusion On expression Tree
a database access implementation mi+ht levera+e this 'acility
to translate expression trees into suitable 0uery statements
'or a particular (ind o' database,
-
8/12/2019 C# 3.0 PPT
94/96
"o int is re0uired as a result lambda has ed+e over dele+ate
-
8/12/2019 C# 3.0 PPT
95/96
-
8/12/2019 C# 3.0 PPT
96/96
+he end