Attention! Your ePaper is waiting for publication!
By publishing your document, the content will be optimally indexed by Google via AI and sorted into the right category for over 500 million ePaper readers on YUMPU.
This will ensure high visibility and many readers!
Your ePaper is now published and live on YUMPU!
You can find your publication here:
Share your interactive ePaper on all platforms and on your website with our embed function
Free-ebooks-library - Bahar Ali Khan
Free-ebooks-library - Bahar Ali Khan
Free-ebooks-library - Bahar Ali Khan
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
C# 4.0<br />
IN A NUTSHELL<br />
Fourth Edition<br />
Joseph Albahari and Ben Albahari<br />
Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo
C# 4.0 IN A NUTSHELL Fourth Edition Joseph Albahari and Ben Albahari Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo
- Page 1 and 2: www.free-ebooks-library.com
- Page 3: C# 4.0 www.free-ebooks-library.com
- Page 7 and 8: Table of Contents Preface . . . . .
- Page 9 and 10: 7. Collections . . . . . . . . . .
- Page 11 and 12: Isolated Storage 573 15. Networking
- Page 13 and 14: 22. Parallel Programming . . . . .
- Page 15 and 16: Preface C# 4.0 further enhances Mic
- Page 17 and 18: Conventions Used in This Book The b
- Page 19 and 20: To comment or ask technical questio
- Page 21 and 22: 1 Introducing C# and the .NET Frame
- Page 23 and 24: C# does not eliminate pointers: it
- Page 25 and 26: What’s New in C# 4.0 The new feat
- Page 27 and 28: In this chapter, we introduce the b
- Page 29 and 30: An array (such as string[]) represe
- Page 31 and 32: Here is the full list of C# keyword
- Page 33 and 34: constant represents an invariant (w
- Page 35 and 36: Immediately after the new operator
- Page 37 and 38: • Generic type parameters • Poi
- Page 39 and 40: Figure 2-4. Assignment copies a ref
- Page 41 and 42: Numeric Types C# has the predefined
- Page 43 and 44: Numeric Conversions Integral to int
- Page 45 and 46: int c = checked (a * b); // Checks
- Page 47 and 48: NaNs are sometimes useful in repres
- Page 49 and 50: type members, by comparing their un
- Page 51 and 52: string a = "Heat"; string is a refe
- Page 53 and 54: Value types versus reference types
- Page 55 and 56:
The second approach is to use the v
- Page 57 and 58:
Console.WriteLine (ref1); // The St
- Page 59 and 60:
Passing arguments by value By defau
- Page 61 and 62:
• Must be assigned before it come
- Page 63 and 64:
To do the converse (pass a default
- Page 65 and 66:
Operators in C# are classed as unar
- Page 67 and 68:
Category Operator symbol Operator n
- Page 69 and 70:
string someWord = "rosebud"; int so
- Page 71 and 72:
if (2 + 2 == 4) Console.WriteLine (
- Page 73 and 74:
This feature of a switch statement
- Page 75 and 76:
The break statement The break state
- Page 77 and 78:
Namespaces are independent of assem
- Page 79 and 80:
Repeated namespaces All type names
- Page 81 and 82:
{ Widget w = new Widget(); } } The
- Page 83 and 84:
In this chapter, we will delve into
- Page 85 and 86:
Overloading methods A type may over
- Page 87 and 88:
public class Class1 { Class1() {} /
- Page 89 and 90:
The this reference also disambiguat
- Page 91 and 92:
} } Notice that you declare the pro
- Page 93 and 94:
public static double Circumference
- Page 95 and 96:
class Class1 { ~Class1() { ... } }
- Page 97 and 98:
public class House : Asset // inher
- Page 99 and 100:
The as operator The as operator per
- Page 101 and 102:
} } Hiding Inherited Members A base
- Page 103 and 104:
Constructors and Inheritance A subc
- Page 105 and 106:
The object Type If you cast Asset t
- Page 107 and 108:
Static and Runtime Type Checking C#
- Page 109 and 110:
Structs A struct is similar to a cl
- Page 111 and 112:
Friend Assemblies In advanced scena
- Page 113 and 114:
Extending an Interface return new C
- Page 115 and 116:
public class RichTextBox : TextBox,
- Page 117 and 118:
In a typical scenario, Bird and Ins
- Page 119 and 120:
[Flags] public enum BorderSides { L
- Page 121 and 122:
public class TopLevel { static int
- Page 123 and 124:
} } Why Generics Exist Generics exi
- Page 125 and 126:
Or: var myDic = new Dictionary(); G
- Page 127 and 128:
The class constraint and struct con
- Page 129 and 130:
The most common scenario is when yo
- Page 131 and 132:
Arrays For historical reasons, arra
- Page 133 and 134:
To give another example, consider t
- Page 135 and 136:
4 Advanced C# In this chapter, we c
- Page 137 and 138:
foreach (int i in values) Console.W
- Page 139 and 140:
public delegate void ProgressReport
- Page 141 and 142:
... static void Main() { int[] valu
- Page 143 and 144:
Return type compatibility If you ca
- Page 145 and 146:
Third, the compiler translates += a
- Page 147 and 148:
public class Stock { ... public eve
- Page 149 and 150:
Event Accessors An event’s access
- Page 151 and 152:
Internally, the compiler resolves l
- Page 153 and 154:
static void Main() { Func natural =
- Page 155 and 156:
A finally block adds determinism to
- Page 157 and 158:
catch (IndexOutOfRangeException ex)
- Page 159 and 160:
Throwing Exceptions Exceptions can
- Page 161 and 162:
System.ArgumentOutOfRangeException
- Page 163 and 164:
Alternatives to exceptions As with
- Page 165 and 166:
Iterators Whereas a foreach stateme
- Page 167 and 168:
Iterators and try/catch/finally blo
- Page 169 and 170:
To represent null in a value type,
- Page 171 and 172:
Further: • If exactly one operand
- Page 173 and 174:
Alternatives to Nullable Types Befo
- Page 175 and 176:
Overloading Equality and Comparison
- Page 177 and 178:
if (x.m_value == False.m_value) ret
- Page 179 and 180:
Extension methods versus instance m
- Page 181 and 182:
Two anonymous type instances will h
- Page 183 and 184:
} } public class Duck : DynamicObje
- Page 185 and 186:
Reflecting on a type exposing (publ
- Page 187 and 188:
In the following example, the parti
- Page 189 and 190:
Console.WriteLine (f.GetType().Name
- Page 191 and 192:
Pointer Basics For every value type
- Page 193 and 194:
Fixed-size buffers Memory can be al
- Page 195 and 196:
Table 4-1. Preprocessor directives
- Page 197 and 198:
• Third-party tools (such as Sand
- Page 199 and 200:
is on the tag (in which it verifie
- Page 201 and 202:
5 Framework Overview Almost all the
- Page 203 and 204:
concurrent collections, and low-lat
- Page 205 and 206:
System.Xml.Xsl // Stylesheet suppor
- Page 207 and 208:
also describe how to use Remoting t
- Page 209 and 210:
WPF’s size and complexity, howeve
- Page 211 and 212:
Workflow came originally with .NET
- Page 213 and 214:
6 Framework Fundamentals Many of th
- Page 215 and 216:
Static method Characters included U
- Page 217 and 218:
Manipulating strings Because String
- Page 219 and 220:
Comparing Strings A disadvantage of
- Page 221 and 222:
For example: Console.WriteLine (str
- Page 223 and 224:
Setting a StringBuilder’s Length
- Page 225 and 226:
UTF-8 is the default text encoding
- Page 227 and 228:
For example: Console.WriteLine (new
- Page 229 and 230:
Internally, DateTimeOffset uses a s
- Page 231 and 232:
• The LocalDateTime property retu
- Page 233 and 234:
Null DateTime and DateTimeOffset va
- Page 235 and 236:
Console.WriteLine (zone.GetUtcOffse
- Page 237 and 238:
3. Otherwise, if IsFixedDateRule is
- Page 239 and 240:
Despite loc1 and loc2 reporting as
- Page 241 and 242:
NumberFormatInfo f = new NumberForm
- Page 243 and 244:
string.Format accepts an optional f
- Page 245 and 246:
Console.WriteLine (string.Format (f
- Page 247 and 248:
Specifier Meaning Sample input Resu
- Page 249 and 250:
NumberFormatInfo ni = new NumberFor
- Page 251 and 252:
DateTimeStyles ParseExact is usuall
- Page 253 and 254:
want to round rather than truncate.
- Page 255 and 256:
TypeConverter for the Color type (i
- Page 257 and 258:
Working with Numbers Conversions We
- Page 259 and 260:
The advantage of storing such a num
- Page 261 and 262:
static void Display (Enum value) {
- Page 263 and 264:
An optional third argument lets you
- Page 265 and 266:
You can also pass in a custom equal
- Page 267 and 268:
Uri uri1 = new Uri ("http://www.lin
- Page 269 and 270:
{ if (!object.Equals (newValue, _va
- Page 271 and 272:
Equality and Custom Types Recall de
- Page 273 and 274:
In contrast, the default GetHashCod
- Page 275 and 276:
Here’s another way to implement t
- Page 277 and 278:
and > When implementing the ICompar
- Page 279 and 280:
via Read, ReadKey, and ReadLine, an
- Page 281:
You can do the same to invoke the c
- Page 284 and 285:
pair of interfaces (IEnumerable, IE
- Page 286 and 287:
It’s a standard practice for coll
- Page 288 and 289:
practice, we’ve implemented the n
- Page 290 and 291:
public bool MoveNext() { return ++c
- Page 292 and 293:
There is no consistent rationale in
- Page 294 and 295:
pseudotype appropriate to the array
- Page 296 and 297:
elements) via several methods that
- Page 298 and 299:
This uses an Action delegate, with
- Page 300 and 301:
Exists returns true if any array me
- Page 302 and 303:
The Copy and CopyTo methods are ove
- Page 304 and 305:
public ReadOnlyCollection AsReadOnl
- Page 306 and 307:
public void AddAfter (LinkedListNod
- Page 308 and 309:
Console.WriteLine (q.Dequeue()); //
- Page 310 and 311:
The commonality of these types is c
- Page 312 and 313:
Dictionaries A dictionary is a coll
- Page 314 and 315:
• Retrieving a nonexistent key vi
- Page 316 and 317:
ListDictionary and HybridDictionary
- Page 318 and 319:
Customizable Collections and Proxie
- Page 320 and 321:
protected override void InsertItem
- Page 322 and 323:
public int Popularity; public Zoo Z
- Page 324 and 325:
t.Names.Add ("test"); // Compiler e
- Page 326 and 327:
public class LastFirstEqComparer :
- Page 328 and 329:
The object.Equals check ensures tha
- Page 330 and 331:
IStructuralEquatable and IStructura
- Page 332 and 333:
Queries that operate over local seq
- Page 334 and 335:
Fluent Syntax Fluent syntax is the
- Page 336 and 337:
Console.WriteLine(); foreach (strin
- Page 338 and 339:
For example, consider the signature
- Page 340 and 341:
Query Expressions C# provides a syn
- Page 342 and 343:
method names yourself. This offers
- Page 344 and 345:
int matches = names.Where (n => n.C
- Page 346 and 347:
This can be a real trap when buildi
- Page 348 and 349:
This method is an iterator by virtu
- Page 350 and 351:
Figure 8-6. Execution of a local qu
- Page 352 and 353:
Figure 8-7. Subquery composition In
- Page 354 and 355:
Progressive Query Building At the s
- Page 356 and 357:
var query = from n1 in names select
- Page 358 and 359:
Anonymous Types Anonymous types all
- Page 360 and 361:
These LINQ-to-db technologies are v
- Page 362 and 363:
Because Queryable.Where also return
- Page 364 and 365:
For instance, suppose we write a cu
- Page 366 and 367:
The problem is that SQL Server does
- Page 368 and 369:
A class decorated with the [Table]
- Page 370 and 371:
The EDM is complex: a thorough disc
- Page 372 and 373:
it’s context.Customers and not co
- Page 374 and 375:
Associations In a multitier applica
- Page 376 and 377:
• With L2S/EF, the subquery is ex
- Page 378 and 379:
DataLoadOptions options = new DataL
- Page 380 and 381:
When you remove a row from an Entit
- Page 382 and 383:
Func predicate1 = p => !p.Disconti
- Page 384 and 385:
From Framework 4.0, this namespace
- Page 386 and 387:
The easiest way to figure out which
- Page 388 and 389:
[Table] public class Purchase { [Co
- Page 390 and 391:
Joining IEnumerable, IEnumerable→
- Page 392 and 393:
The “SQL equivalents” column in
- Page 394 and 395:
WHERE x IN (..., ..., ...) in LINQ
- Page 396 and 397:
Query syntax select projection-expr
- Page 398 and 399:
var query = from c in dataContext.C
- Page 400 and 401:
Figure 9-2. Projecting an object hi
- Page 402 and 403:
Note that the additional generator
- Page 404 and 405:
RESULT: { "Tom vs Tom", "Tom vs Jay
- Page 406 and 407:
}; select new { p.Description, p.Pr
- Page 408 and 409:
Overview Join and GroupJoin mesh tw
- Page 410 and 411:
In query syntax, variables from ear
- Page 412 and 413:
from c in customers join p in purch
- Page 414 and 415:
GroupJoin is equivalent to reading
- Page 416 and 417:
LINQ also provides OrderByDescendin
- Page 418 and 419:
foreach (IGrouping grouping in quer
- Page 420 and 421:
Grouping by multiple keys You can g
- Page 422 and 423:
Conversion Methods LINQ deals prima
- Page 424 and 425:
Cast has query syntax support: simp
- Page 426 and 427:
In LINQ to SQL and EF, Single is of
- Page 428 and 429:
Sum and Average are aggregation ope
- Page 430 and 431:
We can fix this in a number of ways
- Page 432 and 433:
Generation Methods void→IEnumerab
- Page 434 and 435:
What Is a DOM? Consider the followi
- Page 436 and 437:
Hello world Within the parent e
- Page 438 and 439:
Instantiating an X-DOM Rather than
- Page 440 and 441:
Before calling ToString on an arbit
- Page 442 and 443:
This is the output: HammerRasp. Saw
- Page 444 and 445:
IEnumerable query = from c in bench
- Page 446 and 447:
Simple Value Updates Members Works
- Page 448 and 449:
The following removes all customers
- Page 450 and 451:
Casting to a nullable type won’t
- Page 452 and 453:
The next example produces a simple
- Page 454 and 455:
Whatever you specify for the XML ve
- Page 456 and 457:
Joe To give firstname the OReilly.
- Page 458 and 459:
The X-DOM and Default Namespaces Th
- Page 460 and 461:
XNamespace xsi = "http://www.w3.org
- Page 462 and 463:
We then turn this into a projection
- Page 464 and 465:
var customers = new XStreamingEleme
- Page 466 and 467:
static IEnumerable ExpandPaths (IEn
- Page 468 and 469:
XmlReader XmlReader is a high-perfo
- Page 470 and 471:
Two string properties on XmlReader
- Page 472 and 473:
We could read it in as follows: Xml
- Page 474 and 475:
The ReadElementContentAsXXX methods
- Page 476 and 477:
id=123 status=archived Namespaces a
- Page 478 and 479:
Writing Attributes You can write at
- Page 480 and 481:
FirstName = r.ReadElementContentAsS
- Page 482 and 483:
If there were 1 million logentry el
- Page 484 and 485:
To save a document, call Save with
- Page 486 and 487:
customer.AppendChild (lastname); cu
- Page 488 and 489:
The extension methods for use with
- Page 490 and 491:
OUTPUT: Jim Thomas Querying with Na
- Page 492 and 493:
Performing Schema Validation You ca
- Page 494 and 495:
To validate an XmlDocument already
- Page 496 and 497:
The compiler converts this to: File
- Page 498 and 499:
• When an object’s Dispose meth
- Page 500 and 501:
You could describe this as an owner
- Page 502 and 503:
an indeterminate delay between an o
- Page 504 and 505:
Here are some guidelines for implem
- Page 506 and 507:
collection for other objects. We wa
- Page 508 and 509:
memory fragmentation, and it allows
- Page 510 and 511:
Concurrent and background collectio
- Page 512 and 513:
class Client { Host _host; public C
- Page 514 and 515:
the timer. This can create a differ
- Page 516 and 517:
_weakCache = new WeakReference (...
- Page 518 and 519:
The following illustrates how to co
- Page 520 and 521:
static void Main() { #if TESTMODE C
- Page 522 and 523:
Another benefit of [Conditional] is
- Page 524 and 525:
TraceListener The Debug and Trace c
- Page 526 and 527:
It’s a good policy to set AutoFlu
- Page 528 and 529:
The disadvantage of using code cont
- Page 530 and 531:
See “Dealing with Contract Failur
- Page 532 and 533:
To illustrate the member accessibil
- Page 534 and 535:
The code that precedes EndContractB
- Page 536 and 537:
Contract.Result and Contract.ValueA
- Page 538 and 539:
Support for multiple object invaria
- Page 540 and 541:
There are a couple of nuances to be
- Page 542 and 543:
Preconditions (Level 2) Enables all
- Page 544 and 545:
The ContractVerification Attribute
- Page 546 and 547:
Examining Running Processes The Pro
- Page 548 and 549:
To obtain the stack trace for anoth
- Page 550 and 551:
Monitoring the Event Log You can be
- Page 552 and 553:
let instances = cat.GetInstanceName
- Page 554 and 555:
string eatenPerMin = "Macadamias ea
- Page 557 and 558:
14 Streams and I/O This chapter des
- Page 559 and 560:
An adapter wraps a stream, just lik
- Page 561 and 562:
With Read, you can be certain you
- Page 563 and 564:
Thread Safety As a rule, streams ar
- Page 565 and 566:
AppDomain.CurrentDomain.BaseDirecto
- Page 567 and 568:
static MemoryStream ToMemoryStream
- Page 569 and 570:
s.WriteByte (200); // Send the valu
- Page 571 and 572:
Here’s the corresponding client c
- Page 573 and 574:
Text Adapters TextReader and TextWr
- Page 575 and 576:
writer.WriteLine ("Line2"); } using
- Page 577 and 578:
will always use exactly two bytes p
- Page 579 and 580:
Never close a stream before closing
- Page 581 and 582:
Now we can delete the file, for ins
- Page 583 and 584:
sec.AddAccessRule (newRule); File.S
- Page 585 and 586:
we can demonstrate Path’s methods
- Page 587 and 588:
The simplest place to write configu
- Page 589 and 590:
Memory-Mapped Files Memory-mapped f
- Page 591 and 592:
You can also directly access the un
- Page 593 and 594:
The additional flag sent to Deflate
- Page 595 and 596:
Type Computer? Application? Assembl
- Page 597 and 598:
Scope Location Local user \Users\\A
- Page 599 and 600:
15 Networking The Framework offers
- Page 601 and 602:
Table 15-1. Network TLA (three-lett
- Page 603 and 604:
• An absolute path to a file on y
- Page 605 and 606:
4. Call a DownloadXXX or UploadXXX
- Page 607 and 608:
You can also register your own pref
- Page 609 and 610:
The authentication is ultimately ha
- Page 611 and 612:
You can cancel an active WebClient
- Page 613 and 614:
WebClient wc = new WebClient(); wc.
- Page 615 and 616:
var cc = new CookieContainer(); var
- Page 617 and 618:
static bool CertChecker (object sen
- Page 619 and 620:
} HttpListenerContext request = _li
- Page 621 and 622:
In the case of getting a directory
- Page 623 and 624:
Sending Mail with SmtpClient The Sm
- Page 625 and 626:
TcpListener requires the local IP a
- Page 627 and 628:
Client Mail server Notes Client con
- Page 629 and 630:
16 Serialization This chapter intro
- Page 631 and 632:
When Windows Communication Foundati
- Page 633 and 634:
In contrast, implicit serialization
- Page 635 and 636:
using (Stream s = File.Create ("per
- Page 637 and 638:
using (XmlDictionaryReader r = XmlD
- Page 639 and 640:
The DeepClone method we wrote in th
- Page 641 and 642:
Rather than skipping over unrecogni
- Page 643 and 644:
[DataContract] public class Person
- Page 645 and 646:
[DataContract] public class Person
- Page 647 and 648:
Methods decorated with these four a
- Page 649 and 650:
simpler; implementing ISerializable
- Page 651 and 652:
[Serializable] public sealed class
- Page 653 and 654:
[Serializable] public sealed class
- Page 655 and 656:
[Serializable] public class Team :
- Page 657 and 658:
If we started out by writing Person
- Page 659 and 660:
Attributes, names, and namespaces B
- Page 661 and 662:
To demonstrate: Person p = new Pers
- Page 663 and 664:
... ... ... The [XmlArray] attri
- Page 665 and 666:
IXmlSerializable Although attribute
- Page 667 and 668:
17 Assemblies An assembly is the ba
- Page 669 and 670:
Specifying assembly attributes You
- Page 671 and 672:
Figure 17-2. Multifile assembly The
- Page 673 and 674:
the assembly a different identity.
- Page 675 and 676:
The final step is to fully sign the
- Page 677 and 678:
To get an assembly version: string
- Page 679 and 680:
How to Sign with Authenticode Obtai
- Page 681 and 682:
Prior to Framework 4.0, the CLR wou
- Page 683 and 684:
GAC and Versioning Changing an asse
- Page 685 and 686:
csc /resource:, To directly embed a
- Page 687 and 688:
The resource designer adds images a
- Page 689 and 690:
A ResourceManager also lets you enu
- Page 691 and 692:
Framework follows the RFC1766 stand
- Page 693 and 694:
Within the AssemblyResolve event ha
- Page 695 and 696:
The second difference between LoadF
- Page 697 and 698:
AppDomain.CurrentDomain.AssemblyRes
- Page 699:
This allows us to do this: Type t =
- Page 702 and 703:
Reflecting and Activating Types In
- Page 704 and 705:
Nested type names With nested types
- Page 706 and 707:
int i = (int) Activator.CreateInsta
- Page 708 and 709:
At runtime, all generic types are e
- Page 710 and 711:
A MethodHandle is unique to each (g
- Page 712 and 713:
In a C# program, the backing method
- Page 714 and 715:
Type type = typeof (string); Type[]
- Page 716 and 717:
delegate string StringToString (str
- Page 718 and 719:
For instance, suppose we want to wr
- Page 720 and 721:
Group with key=x: x Group with key=
- Page 722 and 723:
minimal space in the metadata (usua
- Page 724 and 725:
3. Write one or more public constru
- Page 726 and 727:
} { mi.Invoke (new Foo(), null); //
- Page 728 and 729:
have replaced the call to EmitWrite
- Page 730 and 731:
When you exit, the evaluation stack
- Page 732 and 733:
Instantiating Objects and Calling I
- Page 734 and 735:
Just as in C#, you can include mult
- Page 736 and 737:
AssemblyBuilder assemBuilder = doma
- Page 738 and 739:
Emitting Methods You can specify a
- Page 740 and 741:
FieldBuilder field = tb.DefineField
- Page 742 and 743:
ILGenerator gen = c.GetILGenerator(
- Page 744 and 745:
The DefineGenericParameters method
- Page 746 and 747:
Although it doesn’t appear so, th
- Page 748 and 749:
TypeBuilder[] uncreatedTypes = { aB
- Page 750 and 751:
static Dictionary _opcodes = new Di
- Page 752 and 753:
string FormatOperand (OpCode c, int
- Page 755 and 756:
19 Dynamic Programming In Chapter 4
- Page 757 and 758:
The actual dynamic call then happen
- Page 759 and 760:
Suppose we want to write a method t
- Page 761 and 762:
} protected override XElement Visit
- Page 763 and 764:
overload when calling GetFooValue w
- Page 765 and 766:
Method Programming construct TryInv
- Page 767 and 768:
DynamicObject also exposes some vir
- Page 769:
You can also get variables back by
- Page 772 and 773:
Although both follow a similar mode
- Page 774 and 775:
A code access security Demand check
- Page 776 and 777:
Table 20-1. Core permissions Type E
- Page 778 and 779:
Type Enforces PublisherIdentityPerm
- Page 780 and 781:
summarize it by saying that securit
- Page 782 and 783:
How the Transparency Model Works In
- Page 784 and 785:
• Security-critical • Security-
- Page 786 and 787:
} } *p++ &= 0xFF; Conversely, you m
- Page 788 and 789:
• Ask the author of X to consider
- Page 790 and 791:
} } You can also pass a list of ass
- Page 792 and 793:
static void Main() { string pluginF
- Page 794 and 795:
The .NET code access permission cla
- Page 796 and 797:
AppDomain.CurrentDomain.SetPrincipa
- Page 798 and 799:
Most types in System.Security.Crypt
- Page 800 and 801:
with each password before hashing.
- Page 802 and 803:
{ using (Aes algorithm = Aes.Create
- Page 804 and 805:
encryptor, CryptoStreamMode.Write )
- Page 806 and 807:
The RSA Class The .NET Framework pr
- Page 808 and 809:
SignHash still needs to know what h
- Page 810 and 811:
Speculative execution On multicore
- Page 812 and 813:
Figure 21-1. Starting a new thread
- Page 814 and 815:
static void Main() { Thread t = new
- Page 816 and 817:
class Introducer { public string Me
- Page 818 and 819:
eal-time work, you must also elevat
- Page 820 and 821:
There are, however, some cases wher
- Page 822 and 823:
The generic Task class is a subclas
- Page 824 and 825:
In the following example, we use an
- Page 826 and 827:
Locking constructs These limit the
- Page 828 and 829:
Blocking Versus Spinning Sometimes
- Page 830 and 831:
Monitor.Enter and Monitor.Exit C#
- Page 832 and 833:
class ThreadUnsafe { static int _x;
- Page 834 and 835:
lock (locker2); // Deadlock } }).St
- Page 836 and 837:
} } static void RunProgram() { Cons
- Page 838 and 839:
Thread safety is hence usually impl
- Page 840 and 841:
Static methods Locking around acces
- Page 842 and 843:
would prevent this, but would creat
- Page 844 and 845:
and we wanted to read/write them at
- Page 846 and 847:
If methods A and B ran concurrently
- Page 848 and 849:
Do We Really Need Locks and Barrier
- Page 850 and 851:
The MSDN documentation states that
- Page 852 and 853:
static void Main() { // _sum // Sim
- Page 854 and 855:
Figure 21-2. Signaling with an Even
- Page 856 and 857:
Figure 21-3 shows this process visu
- Page 858 and 859:
You can reincrement a CountdownEven
- Page 860 and 861:
WaitAll and SignalAndWait have a we
- Page 862 and 863:
Monitor.Pulse (_locker); // or: Mon
- Page 864 and 865:
Pulse executes first, the pulse is
- Page 866 and 867:
Here’s a Main method that starts
- Page 868 and 869:
This forces the blocking condition
- Page 870 and 871:
} static void Speak() { for (int i
- Page 872 and 873:
A class following the EAP may offer
- Page 874 and 875:
class Program { static BackgroundWo
- Page 876 and 877:
Interrupting a thread does not caus
- Page 878 and 879:
We can use this as follows: Operati
- Page 880 and 881:
Most of these classes’ use of can
- Page 882 and 883:
} } You can also pass in another ar
- Page 884 and 885:
using Random (limiting concurrency)
- Page 886 and 887:
The following program demonstrates
- Page 888 and 889:
3. Call EnterWriteLock (this conver
- Page 890 and 891:
The multithreaded timers are more p
- Page 892 and 893:
Both are like System.Timers.Timer i
- Page 894 and 895:
You’ll need to be comfortable wit
- Page 896 and 897:
Partitions work Collates results PL
- Page 898 and 899:
Figure 22-2. PLINQ execution model
- Page 900 and 901:
PLINQ and Ordering You can tweak PL
- Page 902 and 903:
var query = wordsToTest .AsParallel
- Page 904 and 905:
For best performance, any methods c
- Page 906 and 907:
Cancellation Canceling a PLINQ quer
- Page 908 and 909:
e processed on the same thread). If
- Page 910 and 911:
int[] numbers = { 1, 2, 3 }; int su
- Page 912 and 913:
The Parallel Class PFX provides a b
- Page 914 and 915:
Outer versus inner loops The latter
- Page 916 and 917:
do this: Parallel.ForEach ("Hello,
- Page 918 and 919:
We must still lock, but only around
- Page 920 and 921:
Specifying a state object You can t
- Page 922 and 923:
single AggregateException that accu
- Page 924 and 925:
To detect a canceled task, catch an
- Page 926 and 927:
A safe pattern is to rethrow antece
- Page 928 and 929:
• The antecedent was canceled via
- Page 930 and 931:
public partial class MyWindow : Win
- Page 932 and 933:
var source = new TaskCompletionSour
- Page 934 and 935:
childFactory.StartNew (() => 5 / nu
- Page 936 and 937:
performed atomically, eliminating t
- Page 938 and 939:
can force the sequence to end by ca
- Page 940 and 941:
} } catch (Exception ex) { workItem
- Page 942 and 943:
Given SpinLock’s ungainly value-t
- Page 944 and 945:
The correct way to do this without
- Page 947 and 948:
23 Asynchronous Methods In Chapter
- Page 949 and 950:
IAsyncResult BeginXXX (in/ref-args,
- Page 951 and 952:
using System; using System.Threadin
- Page 953 and 954:
if (r.CompletedSynchronously) retur
- Page 955 and 956:
... void MyCallback (IAsyncResult r
- Page 957 and 958:
public class Server { public void S
- Page 959 and 960:
_stream = _client.GetStream(); Task
- Page 961:
The boolean argument at the end ins
- Page 964 and 965:
Figure 24-1. Application domain arc
- Page 966 and 967:
This is acceptable providing the ev
- Page 968 and 969:
The LoaderOptimization Attribute By
- Page 970 and 971:
You can access these properties fro
- Page 972 and 973:
Output: guess what... A slot is cre
- Page 974 and 975:
Because in this example the client
- Page 976 and 977:
public class Discoverer : MarshalBy
- Page 978 and 979:
parameters are converted into null-
- Page 980 and 981:
} public ushort Milliseconds; The S
- Page 982 and 983:
Simulating a C Union Each field in
- Page 984 and 985:
[DllImport ("Kernel32.dll", SetLast
- Page 986 and 987:
data->Letter = 'X'; data->Numbers[1
- Page 988 and 989:
Don’t be put off by the pointers:
- Page 990 and 991:
In a way, the .NET programming mode
- Page 992 and 993:
{ static void Main() { var excel =
- Page 994 and 995:
a COM instance, and then use dynami
- Page 996 and 997:
PIAs, however, were a poor solution
- Page 998 and 999:
item is either a single character o
- Page 1000 and 1001:
Enum value Regular expressions code
- Page 1002 and 1003:
Quantifiers Quantifiers match an it
- Page 1004 and 1005:
The opposite is the negative lookah
- Page 1006 and 1007:
\d{3}-\d{3}-\d{4} Suppose we wish t
- Page 1008 and 1009:
@"""/>"; // "/> Console.Write (Rege
- Page 1010 and 1011:
Matching Roman numerals string r =
- Page 1012 and 1013:
new[] { '+' }, StringSplitOptions.R
- Page 1014 and 1015:
Table 26-7. Zero-width assertions E
- Page 1017 and 1018:
A C# Keywords abstract A class modi
- Page 1019 and 1020:
enum A type declaration keyword tha
- Page 1021 and 1022:
new An operator that calls a constr
- Page 1023 and 1024:
A keyword that allows one construct
- Page 1025 and 1026:
Symbols #define directive, 175 #els
- Page 1027 and 1028:
Assembly class, 651 assembly manife
- Page 1029 and 1030:
object type, 85-88 partial classes,
- Page 1031 and 1032:
parsing numbers in base 2, 8, and 1
- Page 1033 and 1034:
dynamic calls without dynamic recei
- Page 1035 and 1036:
common patterns, 141 common types,
- Page 1037 and 1038:
GetUnicodeCategory method, 195 GetU
- Page 1039 and 1040:
J jagged arrays, 34 Join method, 19
- Page 1041 and 1042:
metadata token, 701 methods, 8-9, 6
- Page 1043 and 1044:
increment and decrement operators,
- Page 1045 and 1046:
Range query operator, 412 range var
- Page 1047 and 1048:
Silverlight, 188 Single query opera
- Page 1049 and 1050:
yte arrays, encoding to, 205 UTF-16
- Page 1051 and 1052:
ushort integral type, 25 using dire
- Page 1053:
XPathNavigator, 469 XSD (XML Schema
Inappropriate
Loading...
Inappropriate
You have already flagged this document.
Thank you, for helping us keep this platform clean.
The editors will have a look at it as soon as possible.
Mail this publication
Loading...
Embed
Loading...
Delete template?
Are you sure you want to delete your template?
DOWNLOAD ePAPER
This ePaper is currently not available for download.
You can find similar magazines on this topic below under ‘Recommendations’.