Upload
nasir1713
View
223
Download
0
Embed Size (px)
Citation preview
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
1/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
2/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
3/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
4/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
5/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
6/1054
mailto:[email protected]://my.safaribooksonline.com/?portal=oreilly8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
7/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
8/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
9/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
10/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
11/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
12/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
13/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
14/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
15/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
16/1054
http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
17/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
18/1054
mailto:[email protected]8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
19/1054
http://my.safaribooksonline.com/?portal=oreillyhttp://www.oreilly.com/mailto:[email protected]8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
20/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
21/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
22/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
23/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
24/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
25/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
26/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
27/1054
http://www.linqpad.net/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
28/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
29/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
30/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
31/1054
Avoiding conflicts
,I\RXUHDOO\ZDQWWRXVHDQLGHQWLILHUWKDWFODVKHVZLWKDNH\ZRUG\RXFDQGRVRE\
TXDOLI\LQJLWZLWKWKH@SUHIL[)RULQVWDQFH
class class {...} // Illegalclass @class {...} // Legal
7KH @V\PEROGRHVQWIRUPSDUWRIWKHLGHQWLILHULWVHOI6R@myVariableLVWKHVDPHDVmyVariable
7KH@SUHIL[FDQEHXVHIXOZKHQFRQVXPLQJOLEUDULHVZULWWHQLQRWKHU1(7ODQJXDJHVWKDWKDYHGLIIHUHQWNH\ZRUGV
C#Basics
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
32/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
33/1054
http://oreilly.com/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
34/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
35/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
36/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
37/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
38/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
39/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
40/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
41/1054
8 bits 27to 271
short Int16 16 bits 215to 2151
int Int32 32 bits 231to 2311
long Int64 L 64 bits 263to 2631
Integralunsigned
byte Byte 8 bits 0 to 281
ushort UInt16 16 bits 0 to 2161
uint UInt32 U 32 bits 0 to 2321
ulong UInt64 UL 64 bits 0 to 2641
Real
float Single F 32 bits (~1045to 1038)
double Double D 64 bits (~10324to 10308)
decimal Decimal M 128 bits (~1028
to 1028
)
2IWKHLQWHJUDOW\SHVint DQGlongDUHILUVWFODVVFLWL]HQVDQGDUHIDYRUHGE\ERWK&
DQGWKHUXQWLPH7KHRWKHULQWHJUDOW\SHVDUHW\SLFDOO\XVHGIRULQWHURSHUDELOLW\RU
ZKHQVSDFHHIILFLHQF\LVSDUDPRXQW
2IWKHUHDOQXPEHUW\SHV float DQGdoubleDUHFDOOHGIORDWLQJSRLQWW\SHV DQGDUH
W\SLFDOO\XVHGIRUVFLHQWLILFFDOFXODWLRQV7KHdecimalW\SHLVW\SLFDOO\XVHG IRUILQDQFLDO FDOFXODWLRQV ZKHUH EDVHDFFXUDWH DULWKPHWLF DQG KLJK SUHFLVLRQ DUH
UHTXLUHG
Numeric Literals
C#Basics
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
42/1054
float f =1.0F;
D double double d =1D;
M decimal decimal d =1.0M;
U uint or ulong Combinable with L uint i =1U;
L long or ulong Combinable with U ulong i =1UL;
7KHVXIIL[HV UDQGL DUHUDUHO\QHFHVVDU\EHFDXVHWKHuint longDQG ulongW\SHVFDQ
QHDUO\DOZD\VEHHLWKHULQIHUUHGRULPSOLFLWO\FRQYHUWHGIURPint
long i =5; // Implicit lossless conversion from int literal to long
7KH D VXIIL[ LV WHFKQLFDOO\ UHGXQGDQW LQ WKDW DOO OLWHUDOV ZLWK D GHFLPDO SRLQW DUHLQIHUUHGWREHdouble$QG\RXFDQDOZD\VDGGDGHFLPDOSRLQWWRDQXPHULFOLWHUDO
double x =4.0;
7KH FDQGMVXIIL[HVDUHWKHPRVWXVHIXODQGVKRXOGDOZD\VEHDSSOLHGZKHQVSHFL
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
43/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
44/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
45/1054
Complement ~0xfU 0xfffffff0U
& And 0xf0 &0x33 0x30
| Or 0xf0 |0x33 0xf3
^ Exclusive Or 0xff00 0x0ff0 0xf0f0
C#Basics
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
46/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
47/1054
Internal representation Base 2 Base 10
Precision 1516 significant f igures 2829 significant f igures
Range (~10324to ~10308) (~1028to ~1028)
Special values +0, 0, +, , and NaN None
Speed Native to processor Non-native to processor (about 10 times slower thandouble)
Real Number Rounding Errors
float DQGdouble LQWHUQDOO\ UHSUHVHQW QXPEHUV LQ EDVH )RU WKLV UHDVRQ RQO\
QXPEHUVH[SUHVVLEOHLQEDVHDUHUHSUHVHQWHGSUHFLVHO\3UDFWLFDOO\WKLVPHDQVPRVW
OLWHUDOVZLWKDIUDFWLRQDOFRPSRQHQWZKLFKDUHLQEDVHZLOOQRWEHUHSUHVHQWHGSUHFLVHO\)RUH[DPSOH
float tenth =0.1f; // Not quite 0.1float one =1f;Console WriteLine (one - tenth * 10f); // 1 490116E-08
C#Basics
http://0.0.0.0/http://www.ieee.org/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
48/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
49/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
50/1054
Single quote 0x0027
\" Double quote 0x0022
\\ Backslash 0x005C
\0 Null 0x0000
\a Alert 0x0007
\b Backspace 0x0008
\f Form feed 0x000C
\n New line 0x000A
\r Carriage return 0x000D
\t Horizontal tab 0x0009
\v Vertical tab 0x000B
7KH\uRU\x HVFDSHVHTXHQFHOHWV\RXVSHFLI\DQ\8QLFRGHFKDUDFWHUYLDLWVIRXU
GLJLWKH[DGHFLPDOFRGH
char copyrightSymbol =\u00A9;
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
51/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
52/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
53/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
54/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
55/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
56/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
57/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
58/1054
All reference types null
All numeric and enum types 0
char type \0
bool type false
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
59/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
60/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
61/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
62/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
63/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
64/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
65/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
66/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
67/1054
G h l Y
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
68/1054
>= Greater than or equal
to
x >=y Yes
is Type is or is subclass of x is y No
as Type conversion x as y No
Equality == Equals x ==y Yes
!= Not equals x !=y Yes
Logical And & And x &y Yes
Logical Xor ^ Exclusive Or x y Yes
Logical Or | Or x |y Yes
Conditional And && Conditional And x &&y Via &
Conditional Or || Conditional Or x ||y Via |
Null coalescing ?? Null coalescing x ??y No
Conditional ?: Conditional isTrue ?thenThisValue : elseThisValue
No
Assignment = Assign x =y No
*= Multiply self by x *=2 Via *
/= Divide self by x /=2 Via /
+= Add to self x +=2 Via +
= Subtract from self x =2 Via
>
&= And self by x &=2 Via &
= Exclusive-Or self by x =2 Via ^ |= Or self by x |=2 Via |
Lambda => Lambda x =>x + 1 No
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
69/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
70/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
71/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
72/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
73/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
74/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
75/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
76/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
77/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
78/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
79/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
80/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
81/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
82/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
83/1054
Preceding the keyword class Attributesandclass modifiers. The non-nested class modifiers are public, internal,abstract, sealed, static, unsafe, and partial
Following YourClassName Generic type parameters, a base class, and interfaces
Within the braces Class members(these are methods,properties, indexers, events, fields, constructors,operator functions, nested types, and a finalizer)
7KLVFKDSWHUFRYHUVDOORIWKHVHFRQVWUXFWVH[FHSWDWWULEXWHVRSHUDWRUIXQFWLRQVDQG
Static modifier static
Access modifiers public internal private protected
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
84/1054
p p p
Inheritance modifier new
Unsafe code modifier unsafe
Read-only modifier readonly
Threading modifier volatile
The readonly modifier
7KH readonlyPRGLILHU SUHYHQWVDILHOGIURPEHLQJPRGLILHGDIWHUFRQVWUXFWLRQ$
UHDGRQO\ILHOGFDQEHDVVLJQHGRQO\LQLWVGHFODUDWLRQRUZLWKLQWKHHQFORVLQJW\SHV
FRQVWUXFWRU
Field initialization
)LHOGLQLWLDOL]DWLRQLVRSWLRQDO$QXQLQLWLDOL]HGILHOGKDVDGHIDXOWYDOXH0\0 nullfalse)LHOGLQLWLDOL]HUVUXQEHIRUHFRQVWUXFWRUV
public int Age =10;
Declaring multiple fields together
)RU FRQYHQLHQFH \RX PD\ GHFODUH PXOWLSOH ILHOGV RI WKH VDPH W\SH LQ D FRPPD
VHSDUDWHGOLVW7KLVLVDFRQYHQLHQWZD\IRUDOOWKHILHOGVWRVKDUHWKHVDPHDWWULEXWHVDQGILHOGPRGLILHUV)RUH[DPSOH
static readonly int legs =8, eyes =1;
Methods
$PHWKRGSHUIRUPVDQDFWLRQLQDVHULHVRIVWDWHPHQWV$PHWKRGFDQUHFHLYHLQSXW
GDWDIURPWKHFDOOHUE\VSHFLI\LQJSDUDPHWHUVDQGRXWSXWGDWDEDFNWRWKHFDOOHUE\
VSHFLI\LQJD UHWXUQW\SH$PHWKRGFDQVSHFLI\D voidUHWXUQW\SHLQGLFDWLQJWKDWLW
GRHVQW UHWXUQ DQ\ YDOXH WR LWV FDOOHU $ PHWKRG FDQ DOVR RXWSXW GDWD EDFN WR WKH
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
85/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
86/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
87/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
88/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
89/1054
Static modifier static
Access modifiers public internal private protected
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
90/1054
Inheritance modifiers new virtual abstract override sealed
Unmanaged code modifiers unsafe extern
Read-only and calculated properties
$SURSHUW\ LVUHDGRQO\LILWVSHFLILHVRQO\DgetDFFHVVRUDQGLWLVZULWHRQO\LILW
VSHFLILHVRQO\DsetDFFHVVRU:ULWHRQO\SURSHUWLHVDUHUDUHO\XVHG
$ SURSHUW\ W\SLFDOO\ KDV D GHGLFDWHG EDFNLQJ ILHOG WR VWRUH WKH XQGHUO\LQJ GDWD
+RZHYHUDSURSHUW\FDQDOVREHFRPSXWHGIURPRWKHUGDWD)RUH[DPSOH
decimal currentPrice, sharesOwned;
public decimal Worth
{ get {return currentPrice * sharesOwned; }}
Automatic properties
7KHPRVWFRPPRQLPSOHPHQWDWLRQIRUDSURSHUW\LVDJHWWHUDQGRUVHWWHUWKDWVLPSO\
UHDGVDQGZULWHVWRDSULYDWHILHOGRIWKHVDPHW\SHDVWKHSURSHUW\$QDXWRPDWLF
SURSHUW\GHFODUDWLRQLQVWUXFWVWKHFRPSLOHUWRSURYLGHWKLVLPSOHPHQWDWLRQ:HFDQUHGHFODUHWKHILUVWH[DPSOHLQWKLVVHFWLRQDVIROORZV
public class Stock{ ... public decimal CurrentPrice {get; set; }}
7KHFRPSLOHUDXWRPDWLFDOO\JHQHUDWHVDSULYDWHEDFNLQJILHOGRIDFRPSLOHUJHQHUDWHG
QDPHWKDWFDQQRWEHUHIHUUHGWR7KHsetDFFHVVRUFDQEHPDUNHG privateLI\RXZDQW
WRH[SRVHWKHSURSHUW\DVUHDGRQO\WRRWKHUW\SHV$XWRPDWLFSURSHUWLHVZHUHLQ
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
91/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
92/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
93/1054
Access modifiers public internal private protected
Inheritance modifier new
Static Constructors$ VWDWLFFRQVWUXFWRUH[HFXWHVRQFHSHU W\SHUDWKHUWKDQRQFHSHULQVWDQFH$W\SH
FDQGHILQHRQO\RQHVWDWLFFRQVWUXFWRUDQGLWPXVWEHSDUDPHWHUOHVVDQGKDYHWKH
VDPH QDPH DV WKH W\SH
CreatingT
ypes
http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
94/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
95/1054
Unmanaged code modifier unsafe
Partial Types and Methods
3DUWLDOW\SHVDOORZDW\SHGHILQLWLRQ WREH VSOLWW\SLFDOO\DFURVV PXOWLSOHILOHV $
FRPPRQVFHQDULRLVIRUDSDUWLDOFODVVWREHDXWRJHQHUDWHGIURPVRPHRWKHUVRXUFH
HJDQ;6DQGIRUWKDWFODVVWREHDXJPHQWHGZLWKDGGLWLRQDOKDQGDXWKRUHG
PHWKRGV)RUH[DPSOH
// PaymentFormGen.cs - auto-generatedpartial class PaymentForm {... }
// PaymentForm.cs - hand-authoredpartial class PaymentForm {... }
(DFKSDUWLFLSDQWPXVWKDYHWKHpartialGHFODUDWLRQWKHIROORZLQJLVLOOHJDOpartial class PaymentForm {}class PaymentForm {}
CreatingT
ypes
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
96/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
97/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
98/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
99/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
100/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
101/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
102/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
103/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
104/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
105/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
106/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
107/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
108/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
109/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
110/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
111/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
112/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
113/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
114/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
115/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
116/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
117/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
118/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
119/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
120/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
121/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
122/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
123/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
124/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
125/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
126/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
127/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
128/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
129/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
130/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
131/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
132/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
133/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
134/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
135/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
136/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
137/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
138/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
139/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
140/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
141/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
142/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
143/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
144/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
145/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
146/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
147/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
148/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
149/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
150/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
151/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
152/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
153/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
154/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
155/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
156/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
157/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
158/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
159/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
160/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
161/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
162/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
163/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
164/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
165/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
166/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
167/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
168/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
169/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
170/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
171/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
172/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
173/1054
http://www.linqpad.net/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
174/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
175/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
176/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
177/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
178/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
179/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
180/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
181/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
182/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
183/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
184/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
185/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
186/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
187/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
188/1054
http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
189/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
190/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
191/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
192/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
193/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
194/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
195/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
196/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
197/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
198/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
199/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
200/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
201/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
202/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
203/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
204/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
205/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
206/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
207/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
208/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
209/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
210/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
211/1054
http://oreilly.com/catalog/9780596520298/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
212/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
213/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
214/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
215/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
216/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
217/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
218/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
219/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
220/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
221/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
222/1054
Gatekeeper class for all CRUD operations DataContext ObjectContext
Type to represent one side of a relationship property, whenthat side has a multiplicity of one
EntityRef EntityReference
Default strategy for loading relationship properties Lazy Explicit
Construct that enables eager loading DataLoadOptions .Include()
Building Query Expressions
6RIDULQWKLVFKDSWHUZKHQZHYHQHHGHGWRG\QDPLFDOO\FRPSRVHTXHULHVZHYH
GRQH VR E\ FRQGLWLRQDOO\ FKDLQLQJ TXHU\ RSHUDWRUV $OWKRXJK WKLV LV DGHTXDWH LQ
PDQ\VFHQDULRVVRPHWLPHV\RXQHHGWRZRUNDWDPRUHJUDQXODUOHYHODQGG\QDP
LFDOO\FRPSRVHWKHODPEGDH[SUHVVLRQVWKDWIHHGWKHRSHUDWRUV
,QWKLVVHFWLRQZHOODVVXPHWKHIROORZLQJProductFODVV
[Table]public partial class Product{ [Column(IsPrimaryKey=true)]public int ID;
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
223/1054
[Co u ( s a y eyt ue)]pub c t D; [Column] public string Description; [Column] public bool Discontinued;
[Column] public DateTime LastSale;}
Delegates Versus Expression Trees
5HFDOOWKDW
/RFDOTXHULHVZKLFKXVHEnumerableRSHUDWRUVWDNHGHOHJDWHV
,QWHUSUHWHGTXHULHVZKLFKXVHQueryableRSHUDWRUVWDNHH[SUHVVLRQWUHHV
:HFDQVHHWKLVE\FRPSDULQJWKHVLJQDWXUHRIWKHWhereRSHUDWRULQEnumerableDQG
Queryable
public static IEnumerableWhere(this IEnumerablesource, Funcpredicate)
public static IQueryableWhere(thisIQueryablesource Expressionpredicate)
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
224/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
225/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
226/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
227/1054
http://www.albahari.com/expressions/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
228/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
229/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
230/1054
http://www.linqpad.net/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
231/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
232/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
233/1054
Returns a subset of elements that satisfy a given condition WHERE
S bl S
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
234/1054
Source sequence IEnumerable
Predicate TSource =>bool or (TSource,int) =>bool aa Prohibited with LINQ to SQL and Entity Framework.
Query syntax
where bool-expression
Enumerable.Where implementation
7KHLQWHUQDOLPSOHPHQWDWLRQRIEnumerable.WhereQXOOFKHFNLQJDVLGHLVIXQFWLRQ
DOO\HTXLYDOHQWWRWKHIROORZLQJ
public static IEnumerableWhere (this IEnumerablesource, Func predicate){ foreach (TSource element in source)
if (predicate (element))yield return element;
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
235/1054
Source sequence IEnumerable
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
236/1054
Source sequence IEnumerable
Number of elements to take or skip int
TakeHPLWVWKHILUVWQ HOHPHQWVDQGGLVFDUGVWKHUHVWSkipGLVFDUGVWKHILUVWQHOHPHQWV
DQGHPLWVWKHUHVW7KHWZRPHWKRGVDUHXVHIXOWRJHWKHUZKHQLPSOHPHQWLQJDZHE
SDJHDOORZLQJ D XVHUWRQDYLJDWHWKURXJK DODUJH VHW RIPDWFKLQJUHFRUGV )RU LQ
VWDQFHVXSSRVHDXVHUVHDUFKHVDERRNGDWDEDVHIRUWKHWHUPPHUFXU\DQGWKHUH
DUHPDWFKHV7KHIROORZLQJUHWXUQVWKHILUVW
IQueryablequery =dataContext.Books .Where (b =>b.Title.Contains ("mercury")).OrderBy (b =>b.Title) .Take (20);
7KHQH[WTXHU\UHWXUQVERRNVWR
IQueryablequery =dataContext.Books
.Where (b =>b.Title.Contains ("mercury"))O d B (b >b Titl )
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
237/1054
Transforms each input element with the given lambda expression SELECT
SelectMany Transforms each input element, and then flattens and concatenates theresultant subsequences
INNER JOIN,
LEFT OUTER JOIN,
CROSS JOIN
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
238/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
239/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
240/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
241/1054
Source sequence IEnumerable
)LJXUH3URMHFWLQJDQREMHFWKLHUDUFK\
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
242/1054
Result selector TSource =>IEnumerableor (TSource,int) =>IEnumerable a
a Prohibited with LINQ to SQL
Query syntax
from identifier1 in enumerable-expression1from identifier2 in enumerable-expression2...
Enumerable implementation
public static IEnumerableSelectMany (IEnumerablesource, Func selector){ foreach (TSource element in source)
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
243/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
244/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
245/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
246/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
247/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
248/1054
Applies a lookup strategy to match elements from two collections, emittinga flat result set
INNER JOIN
GroupJoin As above, but emits a hierarchicalresult set INNER JOIN,
LEFT OUTER JOIN
Join and GroupJoin
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
249/1054
p
Join arguments
Argument Type
Outer sequence IEnumerable
Inner sequence IEnumerable
Outer key selector TOuter =>TKey
Inner key selector TInner =>TKey
Result selector (TOuter,TInner) =>TResult
GroupJoin arguments
Argument Type
+ SelectMany Flat Bad Yes Yes Yes Yes
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
250/1054
Select + Select Nested Bad Yes Yes Yes YesJoin Flat Good Yes - - -
GroupJoin Nested Good Yes Yes - -
GroupJoin + SelectMany Flat Good Yes Yes - -
Join
7KHJoinRSHUDWRUSHUIRUPVDQLQQHUMRLQHPLWWLQJDIODWRXWSXWVHTXHQFH
(QWLW\)UDPHZRUNKLGHVIRUHLJQNH\ILHOGVVR\RXFDQWPDQXDOO\MRLQDFURVVQDWXUDOUHODWLRQVKLSVLQVWHDG\RXFDQTXHU\DFURVVDVVRFLDWLRQSURSHUWLHVDVZHGHVFULEHGLQWKHSUHYLRXV
WZRVHFWLRQV
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
251/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
252/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
253/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
254/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
255/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
256/1054
Input sequence IEnumerable
Key selector TSource =>TKey
5HWXUQW\SHIOrderedEnumerable
ThenBy and ThenByDescending arguments
Argument TypeInput sequence IOrderedEnumerable
Key selector TSource =>TKey
Query syntax
orderby expression1 [descending][, expression2 [descending]... ]
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
257/1054
Overview
OrderByUHWXUQV DVRUWHGYHUVLRQRIWKHLQSXWVHTXHQFHXVLQJWKHkeySelectorH[
SUHVVLRQWRPDNHFRPSDULVRQV7KHIROORZLQJTXHU\HPLWVDVHTXHQFHRIQDPHVLQ
DOSKDEHWLFDORUGHU
IEnumerablequery =names.OrderBy (s =>s);
7KHIROORZLQJVRUWVQDPHVE\OHQJWK
IEnumerablequery =names.OrderBy (s =>s.Length);
// Result: {"Jay", "Tom", "Mary", "Dick", "Harry"};
7KHUHODWLYHRUGHURIHOHPHQWVZLWKWKHVDPHVRUWLQJNH\LQWKLVFDVH-D\7RPDQG
0DU\LFNLVLQGHWHUPLQDWHXQOHVV\RXDSSHQGDThenByRSHUDWRU
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
258/1054
Groups a sequence into subsequences GROUP BY
GroupBy
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
259/1054
GroupBy
Argument Type
Input sequence IEnumerable
Key selector TSource =>TKey
Element selector (optional) TSource =>TElement
Comparer (optional) IEqualityComparer
Query syntax
group element-expression by key-expression
Overview
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
260/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
261/1054
Returns a concatenation of elements in each of the two sequences UNION ALL
Union Returns a concatenation of elements in each of the two sequences,l di d li t
UNION
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
262/1054
q ,excluding duplicates
Intersect Returns elements present in both sequences WHERE ... IN (...)
Except Returns elements present in the first, but not the second sequence EXCEPT
or
WHERE... NOT IN(...)
Concat and Union
ContactUHWXUQV DOO WKHHOHPHQWVRIWKHILUVWVHTXHQFHIROORZHGE\DOOWKHHOHPHQWV
RIWKHVHFRQGUnionGRHVWKHVDPHEXWUHPRYHVDQ\GXSOLFDWHV
int[]seq1 ={1,2,3},seq2={3,4,5};
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
263/1054
Converts IEnumerable to IEnumerable, discarding wrongly typed elementsCast Converts IEnumerable to IEnumerable, throwing an exception if there are any wrongly
typed elements
ToArray Converts IEnumerableto T[]
ToList Converts IEnumerableto List
ToDictionary Converts IEnumerableto Dictionary
ToLookup Converts IEnumerableto ILookup
AsEnumerable Downcasts to IEnumerable
AsQueryable Casts or converts to IQueryable
OfType and Cast
OfType DQG Cast DFFHSW D QRQJHQHULF IEnumerable FROOHFWLRQ DQG HPLW D JHQHULF
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
264/1054
IEnumerableVHTXHQFHWKDW\RXFDQVXEVHTXHQWO\TXHU\
ArrayList classicList =new ArrayList(); // in System.CollectionsclassicList.AddRange ( new int[]{3, 4, 5 });IEnumerablesequence1 =classicList.Cast();
CastDQG OfTypeGLIIHULQWKHLUEHKDYLRUZKHQHQFRXQWHULQJDQLQSXWHOHPHQWWKDWV
RIDQLQFRPSDWLEOHW\SH CastWKURZVDQH[FHSWLRQ OfTypeLJQRUHVWKHLQFRPSDWLEOHHOHPHQW&RQWLQXLQJWKHSUHFHGLQJH[DPSOH
DateTime offender =DateTime.Now;classicList.Add (offender);IEnumerable sequence2 =classicList.OfType(), // OK - ignores offending DateTime sequence3 =classicList.Cast(); // Throws exception
7KH UXOHV IRU HOHPHQW FRPSDWLELOLW\ H[DFWO\ IROORZ WKRVH RI &V i RSHUDWRU DQG
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
265/1054
Input sequence IEnumerableKey selector TSource =>TKey
Element selector (optional) TSource =>TElement
Comparer (optional) IEqualityComparer
ToDictionaryDOVR IRUFHVLPPHGLDWHH[HFXWLRQRIDVHTXHQFHZULWLQJWKHUHVXOWVWR
DJHQHULFDictionary7KH keySelectorH[SUHVVLRQ\RXSURYLGHPXVWHYDOXDWHWRD
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
266/1054
XQLTXH YDOXH IRU HDFK HOHPHQW LQ WKH LQSXW VHTXHQFH RWKHUZLVH DQ H[FHSWLRQ LV
WKURZQ,QFRQWUDVW ToLookupDOORZVPDQ\HOHPHQWVRIWKHVDPHNH\:HGHVFULEH
ORRNXSVLQWKHHDUOLHUVHFWLRQ-RLQLQJZLWKORRNXSVRQSDJH
AsEnumerable and AsQueryable
AsEnumerable XSFDVWVD VHTXHQFHWR IEnumerable IRUFLQJ WKH FRPSLOHUWRELQG
VXEVHTXHQWTXHU\RSHUDWRUVWRPHWKRGVLQEnumerableLQVWHDGRI Queryable)RUDQ
H[DPSOHVHHWKHVHFWLRQ&RPELQLQJ,QWHUSUHWHGDQG/RFDO4XHULHVRQSDJH
LQ&KDSWHU
AsQueryableGRZQFDVWVDVHTXHQFHWR IQueryableLILWLPSOHPHQWVWKDWLQWHUIDFH
2WKHUZLVHLWLQVWDQWLDWHVDQIQueryableZUDSSHURYHUWKHORFDOTXHU\
Equivalent to First/FirstOrDefault, butthrows an exception if thereis more than one match
ElementAt, ElementAtOrDefault Returns the element at thespecified position
Exception thrown
DefaultIfEmpty Returns null ordefault(TSource) ifthe sequence has noelements
OUTER JOIN
0HWKRGVHQGLQJLQ2UHIDXOWUHWXUQ default(TSource)UDWKHU WKDQWKURZLQJDQ
H[FHSWLRQ LI WKH LQSXW VHTXHQFH LV HPSW\ RU LI QR HOHPHQWV PDWFK WKH VXSSOLHGSUHGLFDWH
default(TSource)LV nullIRUUHIHUHQFHW\SHHOHPHQWVRUEODQNXVXDOO\]HURIRU
YDOXHW\SHHOHPHQWV
First, Last, and Single
Argument Type
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
267/1054
Argument Type
Source sequence IEnumerable
Predicate (optional) TSource =>bool
7KHIROORZLQJH[DPSOHGHPRQVWUDWHVFirstDQGLast
int[]numbers ={1, 2, 3, 4, 5 };int first =numbers.First(); // 1int last =numbers.Last(); // 5int firstEven =numbers.First (n =>n %2 ==0); // 2int lastEven =numbers.Last (n =>n %2 ==0); // 4
7KHIROORZLQJGHPRQVWUDWHVFirstYHUVXVFirstOrDefault
i t fi tBi E b Fi t ( > >10) // E ti
Source sequence IEnumerable
Index of element to return int
ElementAtSLFNVWKHQWKHOHPHQWIURPWKHVHTXHQFH
int[]numbers ={1, 2, 3, 4, 5 };
int third =numbers.ElementAt (2); // 3int tenthError =numbers.ElementAt (9); // Exceptionint tenth =numbers.ElementAtOrDefault (9); // 0
Enumerable.ElementAtLV ZULWWHQVXFKWKDWLIWKHLQSXWVHTXHQFHKDSSHQVWRLPSOH
PHQWIListLWFDOOV IListVLQGH[HU2WKHUZLVHLWHQXPHUDWHV Q WLPHVDQG
WKHQUHWXUQVWKHQH[WHOHPHQWElementAtLVQRWVXSSRUWHGLQ/,14WR64/RU()
DefaultIfEmpty
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
268/1054
DefaultIfEmptyFRQYHUWVHPSW\VHTXHQFHVWRQXOO default()7KLVLVXVHGLQZULWLQJ
IODWRXWHUMRLQVVHHWKHHDUOLHUVHFWLRQV2XWHUMRLQVZLWK6HOHFW0DQ\RQSDJH
DQG)ODWRXWHUMRLQVRQSDJH
Aggregation Methods
IEnumerablezscalar
Method Description SQL equivalents
Count, LongCount Returns the number of elements in the input sequence,optionally satisfying a predicate
COUNT (...)
Mi M R t th ll t l t l t i th MIN ( ) MAX ( )
Source sequence IEnumerable
Result selector (optional) TSource =>TResult
MinDQGMaxUHWXUQWKHVPDOOHVWRUODUJHVWHOHPHQWIURPDVHTXHQFH
int[]numbers ={28, 32, 14 };int smallest =numbers.Min(); // 14;
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
269/1054
int smallest numbers.Min(); // 14;int largest =numbers.Max(); // 32;
,I\RXLQFOXGHDselectorH[SUHVVLRQHDFKHOHPHQWLVILUVWSURMHFWHG
int smallest =numbers.Max (n =>n %10); // 8;
$ selector H[SUHVVLRQ LV PDQGDWRU\ LI WKH LWHPV WKHPVHOYHV DUH QRW LQWULQVLFDOO\
FRPSDUDEOHLQRWKHUZRUGVLIWKH\GRQRWLPSOHPHQWIComparable
Purchase runtimeError =dataContext.Purchases.Min (); // Errordecimal?lowestPrice =dataContext.Purchases.Min (p =>p.Price); // OK
$ selectorH[SUHVVLRQGHWHUPLQHVQRWRQO\KRZHOHPHQWVDUHFRPSDUHGEXWDOVR
WKHILQDOUHVXOW,QWKHSUHFHGLQJH[DPSOHWKHILQDOUHVXOWLVDGHFLPDOYDOXHQRWD
SXUFKDVHREMHFW7RJHWWKHFKHDSHVWSXUFKDVH\RXQHHGDVXETXHU\
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
270/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
271/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
272/1054
Returns true if the input sequence contains the given element WHERE ... IN (...)
Any Returns true if any elements satisfy the given predicate WHERE ... IN (...)All Returns true if all elements satisfy the given predicate WHERE (...)
SequenceEqual Returns true if the second sequence has identical elements to the inputsequence
Contains and Any
7KHContainsPHWKRG DFFHSWVDQDUJXPHQWRIW\SH TSource AnyDFFHSWVDQRSWLRQDO
SUHGLFDWH
ContainsUHWXUQVtrueLIWKHJLYHQHOHPHQWLVSUHVHQW
bool hasAThree =new int[]{2, 3, 4 }.Contains (3); // true;
AnyUHWXUQVtrueLIWKHJLYHQH[SUHVVLRQLVWUXHIRUDWOHDVWRQHHOHPHQW:HFDQUHZULWH
WKHSUHFHGLQJTXHU\ZLWKAnyDVIROORZV
bool hasAThree =new int[]{2 3 4 }Any (n =>n ==3); // true;
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
273/1054
bool hasAThree =new int[]{2, 3, 4 }.Any (n =>n ==3); // true;
AnyFDQGRHYHU\WKLQJWKDWContainsFDQGRDQGPRUH
bool hasABigNumber =new int[]{2, 3, 4 }.Any (n =>n >10); // false;
&DOOLQJ AnyZLWKRXWDSUHGLFDWHUHWXUQVtrueLIWKHVHTXHQFHKDVRQHRUPRUHHOH
PHQWV+HUHVDQRWKHUZD\WRZULWHWKHSUHFHGLQJTXHU\
bool hasABigNumber =new int[]{2, 3, 4 }.Where (n =>n >10).Any();
AnyLVSDUWLFXODUO\XVHIXOLQVXETXHULHVDQGLVXVHGRIWHQZKHQTXHU\LQJGDWDEDVHV
IRUH[DPSOH
from c in dataContext.Customerswhere c.Purchases.Any (p =>p.Price >1000)
l
Creates an empty sequence
Repeat Creates a sequence of repeating elementsRange Creates a sequence of integers
Empty RepeatDQG Range DUHVWDWLFQRQH[WHQVLRQPHWKRGVWKDWPDQXIDFWXUHVLPSOH
ORFDOVHTXHQFHV
EmptyEmptyPDQXIDFWXUHVDQHPSW\VHTXHQFHDQGUHTXLUHVMXVWDW\SHDUJXPHQW
foreach (string s in Enumerable.Empty()) Console.Write (s); //
,QFRQMXQFWLRQZLWKWKH??RSHUDWRU EmptyGRHVWKHUHYHUVHRIDefaultIfEmpty)RU
H[DPSOH VXSSRVH ZH KDYH D MDJJHG DUUD\ RI LQWHJHUV DQG ZH ZDQW WR JHW DOO WKH
LQWHJHUVLQWRDVLQJOHIODWOLVW7KHIROORZLQJSelectManyTXHU\IDLOVLIDQ\RIWKHLQQHUDUUD\VLVQXOO
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
274/1054
int[][]numbers ={ new int[]{1, 2, 3 }, new int[]{4, 5, 6 }, null // this null makes the query below fail.
};
IEnumerableflat =numbers.SelectMany (innerArray =>innerArray);
EmptyLQFRQMXQFWLRQZLWK??IL[HVWKHSUREOHP
IEnumerableflat =numbers.SelectMany (innerArray =>innerArray ??Enumerable.Empty ());
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
275/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
276/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
277/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
278/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
279/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
280/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
281/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
282/1054
LastNode {get; } XContainer
IEnumerable Nodes() XContainer*
DescendantNodes() XContainer*
DescendantNodesAndSelf() XElement*
XElement Element (XName) XContainer
IEnumerable Elements() XContainer*
Elements (XName) XContainer*
Descendants() XContainer* Descendants (XName) XContainer*
DescendantsAndSelf() XElement*
DescendantsAndSelf (XName) XElement*
bool HasElements {get; } XElement
)XQFWLRQVPDUNHGZLWKDQDVWHULVNLQWKHWKLUGFROXPQRIWKLVDQGRWKHUWDEOHVDOVRRSHUDWHRQVHTXHQFHVRIWKHVDPHW\SH)RU
LINQtoXML
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
283/1054
)LQVWDQFH \RX FDQ FDOONodes RQ HLWKHU DQXContainer RU D VHTXHQFH RIXContainer REMHFWV 7KLV LV SRVVLEOH EHFDXVH RI H[WHQVLRQPHWKRGVGHILQHGLQSystem.Xml.LinqWKHVXSSOHPHQWDU\TXHU\RSHUDWRUVZHWDONHGDERXWLQWKHRYHUYLHZ
FirstNode, LastNode, and Nodes
FirstNodeDQG LastNodeJLYH\RXGLUHFWDFFHVVWRWKHILUVWRUODVWFKLOGQRGHNodes
UHWXUQVDOOFKLOGUHQDVDVHTXHQFH$OOWKUHHIXQFWLRQVFRQVLGHURQO\GLUHFWGHVFHQG
DQWV)RUH[DPSOH
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
284/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
285/1054
Ancestors (XName) XNode* AncestorsAndSelf() XElement*
AncestorsAndSelf (XName) XElement*
,IxLVDQXElementWKHIROORZLQJDOZD\VSULQWVtrue
foreach (XNode child in x.Nodes())
Console.WriteLine (child.Parent ==x);
7KHVDPHLVQRWWKHFDVHKRZHYHULIxLVDQXDocument XDocumentLVSHFXOLDULWFDQ
KDYHFKLOGUHQ EXWFDQQHYHUEHDQ\RQHVSDUHQW7RDFFHVVWKHXDocument\RXLQVWHDG
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
286/1054
KDYHFKLOGUHQ EXWFDQQHYHUEHDQ\RQHVSDUHQW7RDFFHVVWKHXDocument\RXLQVWHDG
XVHWKHDocumentSURSHUW\WKLVZRUNVRQDQ\REMHFWLQWKH;20WUHH
AncestorsUHWXUQVDVHTXHQFHZKRVHILUVWHOHPHQWLVParentDQGZKRVHQH[WHOHPHQW
LVParent.ParentDQGVRRQXQWLOWKHURRWHOHPHQW
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
287/1054
LastAttribute {get; } XElement
IEnumerable Attributes() XElement
Attributes (XName name) XElement
,QDGGLWLRQXAttributeGHILQHV PreviousAttribute DQGNextAttributeSURSHUWLHVDV
ZHOODVParent
7KHAttributesPHWKRGWKDWDFFHSWVDQDPHUHWXUQVDVHTXHQFHZLWKHLWKHU]HURRU
RQHHOHPHQWDQHOHPHQWFDQQRWKDYHGXSOLFDWHDWWULEXWHQDPHVLQ;0/
, XAttribute
Value {get; set } XElement, XAttribute
7KHSetValuePHWKRG UHSODFHVDQHOHPHQWRUDWWULEXWHVFRQWHQWZLWKDVLPSOHYDOXH6HWWLQJWKHValueSURSHUW\GRHVWKHVDPHEXWDFFHSWVVWULQJGDWDRQO\:HGHVFULEH
ERWKRIWKHVHIXQFWLRQVLQGHWDLOODWHULQWKLVFKDSWHUVHHWKHVHFWLRQ:RUNLQJZLWK
9DOXHVRQSDJH
$QHIIHFWRIFDOOLQJSetValueRUUHDVVLJQLQJ ValueLVWKDWLWUHSODFHVDOOFKLOGQRGHV
XElement settings =new XElement ("settings",
new XElement ("timeout", 30) );settings.SetValue ("blah");Console.WriteLine (settings.ToString()); // blah
Updating Child Nodes and Attributes
Category Members Works onAdd Add (params object[]content) XContainer
AddFirst (params object[]content) XContainer
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
288/1054
AddFirst (params object[]content) XContainer
Remove RemoveNodes() XContainer
RemoveAttributes() XElement
RemoveAll() XElement
Update ReplaceNodes (params object[]content) XContainer
ReplaceAttributes (params object[]content) XElement
ReplaceAll (params object[]content XElement
SetElementValue (XName name, object value) XElement
SetAttributeValue (XName name, object value) XElement
, XAttribute*
ReplaceWith (params object[]content) XNode
7KHPHWKRGV AddBeforeSelf AddAfterSelf RemoveDQG ReplaceWith GRQWRSHUDWH
RQWKHQRGHVFKLOGUHQ,QVWHDGWKH\RSHUDWHRQWKHFROOHFWLRQLQZKLFKWKHQRGH
LWVHOILVLQ7KLVUHTXLUHVWKDWWKHQRGHKDYHDSDUHQWHOHPHQWRWKHUZLVHDQH[FHS
WLRQLVWKURZQAddBeforeSelfDQG AddAfterSelfDUHXVHIXOIRULQVHUWLQJDQRGHLQWR
DQDUELWUDU\SRVLWLRQ
XElement items =new XElement ("items", new XElement ("one"), new XElement ("three")
);items.FirstNode.AddAfterSelf (new XElement ("two"));
+HUHVWKHUHVXOW
LINQtoXML
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
289/1054
,QVHUWLQJLQWRDQDUELWUDU\SRVLWLRQZLWKLQDORQJVHTXHQFHRIHOHPHQWVLVDFWXDOO\
TXLWHHIILFLHQWEHFDXVHQRGHVDUHVWRUHGLQWHUQDOO\LQDOLQNHGOLVW
7KHRemovePHWKRGUHPRYHVWKHFXUUHQWQRGHIURPLWVSDUHQWReplaceWithGRHVWKH
VDPHDQGWKHQLQVHUWVVRPHRWKHUFRQWHQWDWWKHVDPHSRVLWLRQ)RULQVWDQFH
XElement items =XElement.Parse ("");items.FirstNode.ReplaceWith (new XComment ("One was here"));
+HUHVWKHUHVXOW
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
290/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
291/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
292/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
293/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
294/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
295/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
296/1054
http://www.w3.org/2001/xmlschema-instancehttp://www.w3.org/2001/xmlschema-instance8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
297/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
298/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
299/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
300/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
301/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
302/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
303/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
304/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
305/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
306/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
307/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
308/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
309/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
310/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
311/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
312/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
313/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
314/1054
x
ReadString Text x x
ReadElementString Element x x
MoreXML
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
315/1054
ReadElementString Element x x
ReadElementContentAsXXX Element x x
ReadInnerXml Element x x
ReadOuterXml Element x x
ReadStartElement Element x
ReadEndElement Element x
ReadSubtree Element x x
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
316/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
317/1054
...> customer
customer
x:customer
7KHIROORZLQJFRGHZRUNVZLWKWKHILUVWWZRFDVHV
reader.ReadStartElement ("customer");
7KHIROORZLQJLVUHTXLUHGWRKDQGOHWKHWKLUGFDVH
reader.ReadStartElement ("x:customer");
7KHVHFRQG V\VWHPZRUNVWKURXJKWZR QDPHVSDFHDZDUHSURSHUWLHV NamespaceURI
DQG L lN 7KHVH SURSHUWLHV WDNH LQWR DFFRXQW SUHIL[HV DQG GHIDXOW QDPHVSDFHV
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
318/1054
DQGLocalName7KHVHSURSHUWLHVWDNHLQWRDFFRXQWSUHIL[HVDQGGHIDXOWQDPHVSDFHV
GHILQHGE\SDUHQWHOHPHQWV3UHIL[HVDUHDXWRPDWLFDOO\H[SDQGHG7KLVPHDQVWKDWNamespaceURIDOZD\VUHIOHFWVWKHVHPDQWLFDOO\FRUUHFWQDPHVSDFHIRUWKHFXUUHQWHO
HPHQWDQGLocalNameLVDOZD\VIUHHRISUHIL[HV
:KHQ\RXSDVVWZRQDPHDUJXPHQWVLQWRDPHWKRGVXFKDV ReadStartElement\RXUH
XVLQJWKLVVDPHV\VWHP)RUH[DPSOHFRQVLGHUWKHIROORZLQJ;0/
dd
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
319/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
320/1054
http://oreilly.com/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
321/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
322/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
323/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
324/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
325/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
326/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
327/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
328/1054
http://www.w3.org/tr/xpath20/http://www.w3.org/tr/xpath20/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
329/1054
Children
// Recursively children
. Current node (usually implied)
.. Parent node
* Wildcard
@ Attribute
[] Filter: Namespace separator
7RILQGWKHcustomersQRGH
XmlNode node =doc.SelectSingleNode ("customers");
7KH / V\PERO TXHULHV FKLOG QRGHV 7R VHOHFW WKH customer QRGHV
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
330/1054
7KH/V\PEROTXHULHVFKLOGQRGHV7RVHOHFWWKHcustomerQRGHV
XmlNode node =doc.SelectSingleNode ("customers/customer");
7KH // RSHUDWRULQFOXGHV DOOFKLOG QRGHVUHJDUGOHVV RIQHVWLQJ OHYHO 7R VHOHFW DOO
lastnameQRGHV
XmlNodeList nodes =doc.SelectNodes ("//lastname");
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
331/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
332/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
333/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
334/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
335/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
336/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
337/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
338/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
339/1054
Prevents further I/O When you later need to read/write the stream
StreamReader,StreamWriter Flushes the reader/writer andcloses the underlying stream When you want to keep the underlying stream open (youmust instead call Flush on a StreamWriter whenyoure done)
IDbConnection Releases a database connectionand clears the connection string
If you need to re-Open it, you should call Close insteadof Dispose
DataContext(LINQ to SQL)
Prevents further use When you might have lazily evaluated queries connectedto that context
MemoryStreamV DisposePHWKRG GLVDEOHVRQO\WKHREMHFWLWGRHVQWSHUIRUPDQ\FULW
LFDO FOHDQXS EHFDXVH DMemoryStream KROGV QR XQPDQDJHG KDQGOHV RU RWKHU VXFK
UHVRXUFHV
7KHWKLUGFDWHJRU\LQFOXGHVWKHIROORZLQJFODVVHV WebClient StringReader String
WriterDQG BackgroundWorkerLQ System.ComponentModel7KHVHW\SHVDUHGLVSRVD
EOH XQGHU WKH GXUHVV RI WKHLU EDVH FODVV UDWKHU WKDQ WKURXJK D JHQXLQH QHHG WR SHUIRUP
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
340/1054
EOHXQGHUWKHGXUHVVRIWKHLUEDVHFODVVUDWKHUWKDQWKURXJKDJHQXLQHQHHGWRSHUIRUPHVVHQWLDOFOHDQXS,I\RXKDSSHQWRLQVWDQWLDWHDQGZRUNZLWKVXFKDQREMHFWHQWLUHO\
LQ RQH PHWKRGZUDSSLQJ LWLQ Dusing EORFN DGGV OLWWOHLQFRQYHQLHQFH %XWLI WKH
REMHFWLVORQJHUODVWLQJNHHSLQJWUDFNRIZKHQLWVQRORQJHUXVHGVRWKDW\RXFDQ
GLVSRVH RI LW DGGV XQQHFHVVDU\ FRPSOH[LW\ ,Q VXFK FDVHV \RX FDQ VLPSO\ LJQRUH
REMHFWGLVSRVDO
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
341/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
342/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
343/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
344/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
345/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
346/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
347/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
348/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
349/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
350/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
351/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
352/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
353/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
354/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
355/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
356/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
357/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
358/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
359/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
360/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
361/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
362/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
363/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
364/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
365/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
366/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
367/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
368/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
369/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
370/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
371/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
372/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
373/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
374/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
375/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
376/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
377/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
378/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
379/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
380/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
381/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
382/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
383/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
384/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
385/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
386/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
387/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
388/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
389/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
390/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
391/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
392/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
393/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
394/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
395/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
396/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
397/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
398/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
399/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
400/1054
Reading public abstract bool CanRead {get; }
public abstract int Read (byte[]buffer, int offset, int count) public virtual int ReadByte();
Writing public abstract bool CanWrite {get; }
public abstract void Write (byte[]buffer, int offset, int count);
public virtual void WriteByte (byte value);
Seeking public abstract bool CanSeek {get; }
public abstract long Position {get; set; }
public abstract void SetLength (long value);
public abstract long Length {get; }
public abstract long Seek (long offset SeekOrigin origin);
StreamsandI/O
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
401/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
402/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
403/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
404/1054
http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
405/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
406/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
407/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
408/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
409/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
410/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
411/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
412/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
413/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
414/1054
Reading one char public virtual int Peek(); // Cast the result to a char
public virtual int Read(); // Cast the result to a char
Reading manychars
public virtual int Read (char[]buffer, int index, int count);
public virtual int ReadBlock (char[]buffer, int index, int count);
public virtual string ReadLine();
public virtual string ReadToEnd();
Closing public virtual void Close();
public void Dispose(); // Same as Close
Other public static readonly TextReader Null;
public static TextReader Synchronized (TextReader reader);
StreamsandI/O
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
415/1054
Writing one char public virtual void Write (char value);
Writing many chars public virtual void Write (string value);
public virtual void Write (char[]buffer, int index, int count);
public virtual void Write (string format, params object[]arg); public virtual void WriteLine (string value);
Closing and flushing public virtual void Close();
public void Dispose(); // Same as Close
public virtual void Flush();
Formatting and
encoding
public virtual IFormatProvider FormatProvider {get; }
public virtual string NewLine {get; set; }
public abstract Encoding Encoding {get; }
Other public static readonly TextWriter Null;
public static TextWriter Synchronized (TextWriter writer);
WriteLineVLPSO\DSSHQGVWKHJLYHQWH[WZLWK&5/)
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
416/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
417/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
418/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
419/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
420/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
421/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
422/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
423/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
424/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
425/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
426/1054
chars 0 to 31 and "|
Path.GetInvalidFileNameChars() chars 0 to 31 and "|:*?\/
Temporary files:
Path.GetTempPath() \Temp
Path.GetRandomFileName() d2dwuzjf.dnpPath.GetTempFileName() \Temp\tmp14B.tmp
CombineLVSDUWLFXODUO\XVHIXOLWDOORZV\RXWRFRPELQHDGLUHFWRU\DQGILOHQDPH
RUWZR GLUHFWRULHVZLWKRXW ILUVW KDYLQJ WR FKHFN ZKHWKHU D WUDLOLQJ EDFNVODVK LV
S
treamsandI/O
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
427/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
428/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
429/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
430/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
431/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
432/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
433/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
434/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
435/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
436/1054
AssemblyRoaming
AssemblyMachine
GetMachineStoreForAssembly
7KHUHLVQRVXFKWKLQJDVGRPDLQRQO\LVRODWLRQ,I\RXZDQWWRVKDUHDQLVRODWHG
VWRUHDFURVVDOODVVHPEOLHVZLWKLQDQDSSOLFDWLRQWKHUHVDVLPSOHZRUNDURXQGKRZ
HYHU -XVW H[SRVH D SXEOLF PHWKRG LQ RQH RI WKH DVVHPEOLHV WKDW LQVWDQWLDWHV DQG
UHWXUQVDQIsolatedStorageFileStreamREMHFW$Q\DVVHPEO\FDQDFFHVVDQ\LVRODWHGVWRUH LI JLYHQ DQIsolatedStorageFile REMHFWLVRODWLRQ UHVWULFWLRQV DUH LPSRVHG
XSRQFRQVWUXFWLRQQRWVXEVHTXHQWXVH
6LPLODUO\WKHUHVQRVXFKWKLQJDVPDFKLQHRQO\LVRODWLRQ,I\RXZDQWWRVKDUHDQ
LVRODWHGVWRUHDFURVVDYDULHW\RIDSSOLFDWLRQVWKHZRUNDURXQGLVWRZULWHDFRPPRQ
DVVHPEO\WKDWDOODSSOLFDWLRQVUHIHUHQFHDQGWKHQH[SRVHDPHWKRGRQWKHFRPPRQ
DVVHPEO\ WKDW FUHDWHV DQG UHWXUQV DQ DVVHPEO\LVRODWHG IsolatedStorageFileStream7KHFRPPRQDVVHPEO\PXVWEHVWURQJO\QDPHGIRUWKLVWRZRUN
Reading and Writing Isolated Storage
,VRODWHGVWRUDJHXVHVVWUHDPVWKDWZRUNPXFKOLNHRUGLQDU\ILOHVWUHDPV7RREWDLQ
DQLVRODWHGVWRUDJHVWUHDP\RXILUVWVSHFLI\WKHNLQGRILVRODWLRQ\RXZDQWE\FDOOLQJ
RQH RI WKH VWDWLF PHWKRGV RQIsolatedStorageFileDV VKRZQ SUHYLRXVO\ LQ 7DEOH
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
437/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
438/1054
Local user \Users\\AppData\Local\IsolatedStorage
Roaming user \Users\\AppData\Roaming\IsolatedStorageMachine \ProgramData\IsolatedStorage
)RU:LQGRZV;3
Scope Location
Local user \Documents and Settings\\Local Settings\Application Data\Isolated-Storage
Roaming user \Documents and Settings\\Application Data\IsolatedStorage
Machine \Documents and Settings\All Users\Application Data\IsolatedStorage
7KHVHDUHPHUHO\WKHEDVHIROGHUVWKHGDWDILOHVWKHPVHOYHVDUHEXULHGGHHSLQD
ODE\ULQWKRI VXEGLUHFWRULHVZKRVHQDPHVGHULYHIURPKDVKHGDVVHPEO\QDPHV7KLVLVERWKDUHDVRQWRXVHDQGQRWWRXVHLVRODWHGVWRUDJH2QWKHRQHKDQGLWPDNHV
LVRODWLRQSRVVLEOHDSHUPLVVLRQUHVWULFWHGDSSOLFDWLRQZDQWLQJWRLQWHUIHUHZLWKDQ
RWKHUFDQEHVWXPSHGE\EHLQJGHQLHGDGLUHFWRU\OLVWLQJGHVSLWHKDYLQJWKHVDPH
ILOHV\VWHPULJKWVDVLWVSHHUV2QWKHRWKHUKDQGLWPDNHVDGPLQLVWUDWLRQLPSUDFWLFDO
IURPRXWVLGHWKHDSSOLFDWLRQ6RPHWLPHVLWVKDQG\RUHVVHQWLDOWRHGLWDQ;0/
FRQILJXUDWLRQILOHLQ1RWHSDGVRWKDWDQDSSOLFDWLRQFDQVWDUWXSSURSHUO\,VRODWHG
VWRUDJHPDNHVWKLVLPSUDFWLFDO
Enumerating Isolated Storage
$QIsolatedStorageFileREMHFWDOVRSURYLGHVPHWKRGVIRUOLVWLQJILOHVLQWKHVWRUH
using (IsolatedStorageFile f =IsolatedStorageFile.GetUserStoreForDomain()){
using (var s =new IsolatedStorageFileStream ("f1.x",FileMode.Create,f)) s.WriteByte (123);
using (var s =new IsolatedStorageFileStream ("f2.x",FileMode.Create,f)) s.WriteByte (123);
StreamsandI/O
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
439/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
440/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
441/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
442/1054
DNS Domain Name Service Converts between domain names (e.g., ebay.com) and IP addresses(e.g., 199.54.213.2)
FTP File Transfer Protocol Internet-based protocol for sending and receiving files
HTTP Hypertext Transfer Protocol Retrieves web pages and runs web services
IIS Internet Information Services Microsofts web server software
IP Internet Protocol Network-layer protocol below TCP and UDP
LAN Local Area Network Most LANs use Internet-based protocols such as TCP/IPPOP Post Office Protocol Retrieves Internet mail
SMTP Simple Mail Transfer Protocol Sends Internet mail
TCP Transmission and ControlProtocol
Transport-layer Internet protocol on top of which most higher-layerservices are built
UDP Universal Datagram Protocol Transport-layer Internet protocol used for low-overhead services such
as VoIP
UNC Universal Naming Convention \\computer\sharename\filename
URI Uniform Resource Identifier Ubiquitous resource naming system (e.g., http://www.amazon.comor mailto:[email protected])
URL Uniform Resource Locator Technical meaning (fading from use): subset of URI; popular meaning:synonym of URI
Addresses and Ports
)RUFRPPXQLFDWLRQ WRZRUNDFRPSXWHURUGHYLFHUHTXLUHVDQDGGUHVV7KH,QWHUQHW
XVHVWZRDGGUHVVLQJV\VWHPV
,3Y
&XUUHQWO\ WKH GRPLQDQW DGGUHVVLQJ V\VWHP ,3Y DGGUHVVHV DUH ELWV ZLGH
:KHQVWULQJIRUPDWWHG,3YDGGUHVVHVDUHZULWWHQDVIRXUGRWVHSDUDWHGGHFL
PDOV HJ $Q DGGUHVV FDQ EH XQLTXH LQ WKH ZRUOGRU
XQLTXHZLWKLQDSDUWLFXODUVXEQHWVXFKDVRQDFRUSRUDWHQHWZRUN
Networkin
g
http://ebay.com/http://www.amazon.com/mailto:mailto:[email protected]:mailto:[email protected]://www.amazon.com/http://ebay.com/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
443/1054
http://www.ietf.org/http://www.ietf.org/http://www.ietf.org/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
444/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
445/1054
http://www.albahari.com/nutshell/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
446/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
447/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
448/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
449/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
450/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
451/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
452/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
453/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
454/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
455/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
456/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
457/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
458/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
459/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
460/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
461/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
462/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
463/1054
http://ebay.com/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
464/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
465/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
466/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
467/1054
Client connects... +OKHello there. Welcome message
USER j +OK P d q i d
http://0.0.0.0/http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
468/1054
USER joe +OK Password required.
PASS password +OK Logged in.
LIST +OK
1 1876
2 5412
3 845
.
Lists the ID and file size of each message on the server
RETR 1 +OK 1876 octets
Content of message #1...
.
Retrieves the message with the specified ID
DELE 1 +OK Deleted. Deletes a message from the server
QUIT +OK Bye-bye.
(DFKFRPPDQGDQGUHVSRQVHLVWHUPLQDWHGE\DQHZOLQH&5/)H[FHSWIRUWKH
PXOWLOLQH /,67 DQG 5(75 FRPPDQGV ZKLFK DUHWHUPLQDWHG E\ D VLQJOH GRW RQ D
VHSDUDWHOLQH%HFDXVHZHFDQWXVHStreamReaderZLWK NetworkStreamZHFDQVWDUW
E\ZULWLQJDKHOSHUPHWKRGWRUHDGDOLQHRIWH[WLQDQRQEXIIHUHGIDVKLRQ
static string ReadLine (Stream s){ ListlineBuffer =new List(); while (true) { int b =s.ReadByte(); if (b ==10 ||b
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
469/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
470/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
471/1054
Level of automation *** ***** **** *
Type coupling Choice Tight Loose Loose
Version tolerance ***** *** ***** *****
Preserves object references Choice Yes No ChoiceCan serialize nonpublic fields Yes Yes No Yes
Suitability for interoperable messaging ***** ** **** ****
Flexibility in reading/writing XML files ** - **** *****
Compact output ** **** ** **
Performance *** **** * to *** ***
7KH VFRUHV IRUIXmlSerializable DVVXPH \RXYH KDQG FRGHG RSWLPDOO\ XVLQJ
XmlReaderDQG XmlWriter7KH;0/VHULDOL]DWLRQHQJLQHUHTXLUHVWKDW\RXUHF\FOHWKH
VDPHXmlSerializerREMHFWIRUJRRGSHUIRUPDQFH
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
472/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
473/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
474/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
475/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
476/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
477/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
478/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
479/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
480/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
481/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
482/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
483/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
484/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
485/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
486/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
487/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
488/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
489/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
490/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
491/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
492/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
493/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
494/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
495/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
496/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
497/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
498/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
499/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
500/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
501/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
502/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
503/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
504/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
505/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
506/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
507/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
508/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
509/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
510/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
511/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
512/1054
, GetName Returns the fully qualified name or anAssemblyName object
Assembly Names on page 655
CodeBase, Location Location of the assembly file Resolving and Loading Assem-blies on page 671
Load LoadFrom LoadFile Manually loads an assembly into the Resolving and Loading Assem
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
513/1054
Load, LoadFrom, LoadFile Manually loads an assembly into thecurrent application domain
Resolving and Loading Assem-blies on page 671
GlobalAssemblyCache Indicates whether the assembly is de-fined in the GAC
The Global AssemblyCache on page 661
GetSatelliteAssembly Locates the satellite assembly of agiven culture
Resources and Satellite Assem-blies on page 663
GetType, GetTypes Returns a type, or all types, defined inthe assembly
Reflecting and ActivatingTypes on page 682
EntryPoint Returns the applications entrymethod, as a MethodInfo
Reflecting and Invoking Mem-bers on page 688
GetModules,ManifestModule
Returns all modules, or the main mod-ule, of an assembly
Reflecting Assemblieson page 700
GetCustomAttributes Returns the assemblys attributes Working with Attrib-utes on page 701
Strong Names and Assembly Signing
$ VWURQJO\ QDPHG DVVHPEO\ KDV D XQLTXH DQG XQWDPSHUDEOH LGHQWLW\ ,W ZRUNV E\
DGGLQJWZRELWVRIPHWDGDWDWRWKHPDQLIHVW
$XQLTXHQXPEHUWKDWEHORQJVWRWKHDXWKRUVRIWKHDVVHPEO\
$ VLJQHGKDVKRIWKHDVVHPEO\SURYLQJWKDWWKHXQLTXHQXPEHUKROGHUSURGXFHG
WKHDVVHPEO\
7KLVUHTXLUHVDSXEOLFSULYDWHNH\SDLU7KH SXEOLFNH\SURYLGHVWKHXQLTXHLGHQWL
I\LQJQXPEHUDQGWKHSULYDWHNH\IDFLOLWDWHVVLJQLQJ
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
514/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
515/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
516/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
517/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
518/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
519/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
520/1054
http://www.ksoftware.net/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
521/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
522/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
523/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
524/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
525/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
526/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
527/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
528/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
529/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
530/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
531/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
532/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
533/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
534/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
535/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
536/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
537/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
538/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
539/1054
http://0.0.0.0/8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
540/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
541/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
542/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
543/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
544/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
545/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
546/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
547/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
548/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
549/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
550/1054
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
551/1054
Event GetEvent Event name EventInfo
Constructor GetConstructor ConstructorInfo
Finalizer GetMethod "Finalize" MethodInfo
Operator GetMethod "op_"+ operator name MethodInfo
Nested type GetNestedType Type name Type
(DFK MemberInfoVXEFODVV KDVDZHDOWKRISURSHUWLHVDQGPHWKRGVH[SRVLQJDOODV
SHFWVRIWKHPHPEHUVPHWDGDWD7KLVLQFOXGHVVXFKWKLQJVDVYLVLELOLW\PRGLILHUV
JHQHULFW\SHDUJXPHQWVSDUDPHWHUVUHWXUQW\SHDQGFXVWRPDWWULEXWHV
8/12/2019 2010 - C# 4.0 in a Nutshell 4ed (O'Reilly)
552/1054
JHQHULFW\SHDUJXPHQWVSDUDPHWHUVUHWXUQW\SHDQGFXVWRPDWWULEXWHV
+HUHLVDQH[DPSOHRIXVLQJGetMethod
MethodInfo m =typeof (Walnut).GetMethod ("Crack");Console.WriteLine (m); // Void Crack()Console.WriteLine (m.ReturnType); // System.Void
$OO*InfoLQVWDQFHVDUHFDFKHGE\WKHUHIOHFWLRQ$3,RQILUVWXVH
MethodInfo method =typeof (Walnut).GetMethod ("Crack");MemberInfo member =typeof (Walnut).GetMember ("Crack") [0];
Console.Write (method ==member); // True
$VZHOODVSUHVHUYLQJREMHFWLGHQWLW\FDFKLQJLPSURYHVWKHSHUIRUPDQFHRIZKDWLV
RWKHUZLVHDIDLUO\VORZ$3,
C# Members Versus CLR Members
7KHSUHFHGLQJWDEOHLOOXVWUDWHVWKDWVRPHRI&VIXQFWLRQDOFRQVWUXFWVGRQWKDYH
DPDSSLQJZLWK&/5FRQVWUXFWV7KLVPDNHVVHQVHEHFDXVHWKH&/5DQGUHIOHF
WLRQ$3,ZHUHGHVLJQHGZLWKDOO1(7ODQJXDJHVLQPLQG\RXFDQXVHUHIOHFWLRQ
HYHQIURP9LVXDO%DVLF
6RPH&FRQVWUXFWVQDPHO\LQGH[HUVHQXPVRSHUDWRUVDQGILQDOL]HUVDUHFRQ
WULYDQFHVDVIDUDVWKH&/5LVFRQFHUQHG6SHFLILFDOO\