Main()
method
declaring, 10
definition, 9
disambiguating multiple Main()
methods, 182
invoking location, 183
multiple, disambiguating, 182
nonzero return code, 10
passing command-line arguments to, 180–183
main()
method, Java vs. C#, 10
__ makeref
keyword, 8
MakeValue()
method, 480
Managed code, definition, 26
Managed execution
BCL (Base Class Library), 27, 28
CIL (Common Intermediate Language), 26–28
CLI (Common Language Infrastructure) specification, 26–27
CLS (Common Language Specification), 27
code access security, 27
CTS (Common Type System), 27
definition, 26
execution time, 27
garbage collection, 28
just-in-time compilation, 26
language interoperability, 27
managed code, definition, 26
native code, definition, 26
platform portability, 28
type safety, 27
unmanaged code, definition, 26
VES (Virtual Execution System), 26
Many-to-many relationships, definition, 604
Masks, 132
MaxDegreeOfParallelism
property, 807
mcs.exe
compiler, 3
Me
keyword, 230
Member invocation, reflection, 687–692
Member names, retrieving, 729
Member variables, 225
Memory, deallocating, 423. See also Garbage collector.
Metadata
about assemblies, adding, 697–698
within an assembly, examining. See Reflection.
CIL (Common Intermediate Language), 894–895
definition, 25
for types, accessing with System.Type
, 685. See also Reflection.
XML, 25
Metadata tables, setting bits or fields in. See Pseudoattribute.
during construction, C++ vs. C#, 307
as ref
or out
parameter values, 252
vs. statements, 169
translating query expressions to, 640–641
Method group conversion, delegates, 512
Method names
as arguments, delegates, 511–512
calling, 167
Method returns, multicast delegates, 558
MethodCallExpression
, 535
MethodImplAttribute
, 827
MethodImplOptions.Synchronized()
method, 827
Methods. See also specific methods.
accessing by name, on interfaces, 336
class association, 163
declaring in interfaces, 327
derived from System.Object
, 320–321
global, C++ vs. C#, 171
guidelines for naming, 163
identifying support for generics, 692–693
naming conventions, 163
operational polymorphism, 195
overriding, 304
return type declaration, 172–174
uniqueness, 195
applicable calls, 201
caller, 163
compatible calls, 201
method call example, 164
method name, 167
named arguments, 199
namespaces, 164–166. See also specific namespaces.
recursively. See Recursion.
return values, 163
scope, 167
statements vs. method calls, 169
expression bodied methods, 174
formal parameter declaration, 171–172
formal parameter list, 172
method return type declaration, 172–174
refactoring, 171
return
statement, example, 173
type parameter list, 172
void
as a return type, 173–174
Methods, extension
derivation, 299
Microsoft IL (MSIL). See CIL (Common Intermediate Language).
Microsoft Silverlight compiler, 880
Miller, J., 26
Minus sign (-
)
arithmetic subtraction operator, 91–92
precedence, 92
subtraction operator, overloading, 397–399
Minus sign, equal (-=)
binary/assignment operator, 399
Minus signs (--
) decrement operator
C++ vs. C#, 105
guidelines, 105
postfix increment operator, 104–105
post-increment operator, 103
prefix increment operator, 104–105
pre-increment operator, 103–104
Modules, referencing, 405
Monads, definition, 591
Montoya, Inigo, 2
MSIL (Microsoft IL). See CIL (Common Intermediate Language).
MTA (Multithreaded Apartment), 846
Multicast delegates
adding methods to, 554
chaining, 555
internals, 554
new delegate instances, 550
passing by reference, 558
removing delegates from a chain, 550–552
Multidimensional arrays, 74, 77–79
Multiple inheritance
C++ vs. C#, 299
Multiple inheritance, interfaces
diagramming, 345
working around single inheritance, 343–344
Multiple Main()
methods, disambiguating, 182
Multiplication with bit shifting, 130
Multithreaded Apartment (MTA), 846
Multithreaded programming complexities, 749–750
Multithreaded programs, definition, 733
Multithreading
asynchronous operations, definition, 736
clock speeds over time, 732
concurrent operations, definition, 736
context switch, definition, 736
CPU (central processing unit), definition, 734
flow of control, definition, 734
Hyper-Threading, definition, 734
I/O-bound latency, definition, 732
latency, definition, 732
multithreaded programs, definition, 733, 734
parallel programming, definition, 736
PLINQ (Parallel LINQ), 733
process, definition, 734
processor-bound latency, definition, 732
quantum, definition, 736
simultaneous multithreading, definition, 734
single-threaded programs,
definition, 734
TAP (Task-based Asynchronous Pattern), 733
task, definition, 735
thread, definition, 734
thread pool, definition, 735
thread safe code, definition, 734
threading model, definition, 734
time slice, definition, 736
time slicing, definition, 736
TPL (Task Parallel Library), 733
Multithreading, asynchronous tasks
antecedent tasks, 757
associating data with tasks, 755
asynchronous continuations, 756–762
chaining tasks, 757
cold tasks, 752
composing large tasks from smaller one, 756–758
continuation tasks, 757
control flow, 755
creating threads and tasks, 750–751
hot tasks, 752
Id
property, 755
multithreaded programming complexities, 749–750
observing unhandled exceptions, 764–765
registering for notification of task behavior, 760–761
registering for unhandled exceptions, 766–768
synchronous delegates, 751
task identification, 755
task status, getting, 754
tasks, definition, 750
tasks vs. delegates, 751
unhandled exception handling with AggregateException
, 762–765
unhandled exceptions on a thread, 765–768
Multithreading, canceling tasks
cooperative cancellation, definition, 769
disposable tasks, 774
aborting threads, 747
parallel loops, 801
thread pooling, 749
Thread.Sleep()
method, 745
unhandled exceptions, 768
Multithreading, parallel loop iterations
breaking, 808
exception handling with AggregateException
, 803–804
TPL performance tuning, 802–803
Multithreading, performance
context switching, 737
switching overhead, 737
time slicing costs, 737
Multithreading, PLINQ queries
Multithreading, task-based asynchronous pattern
async
and await
with the Windows UI, 795–798
async
keyword, purpose of, 786
asynchronous high-latency operations with the TPL, 777–781
awaiting non-Task<T>
values, 791–792
control flow misconceptions, 784
control flow within tasks, 784–786
custom asynchronous methods, 787–791
synchronization context, 793–795
synchronous high-latency operations, 775–777
task drawbacks, overview, 775
Multithreading, threading problems
atomic operations, 738
complex memory models, 739
deadlocks, 740
lock
statement, 740
locking leading to deadlocks, 740
race conditions, 738
Multithreading, with System.Threading.Thread
asynchronous delays, 745
asynchronous operations, 741–743
await
operator, 745
checking threads for life, 744
foreground threads vs. background, 743
IsAlive
property, 744
IsBackground
property, 743
Priority
property, 743
putting threads to sleep, 744
reprioritizing threads, 743
Task.Delay()
method, 745
Thread.Sleep()
method, putting threads to sleep, 744
ThreadState
property, 744
waiting for threads, 743
Name
property, 249–250, 292, 297, 302–303
Named arguments, calling methods, 199
Named parameters, attributes, 707
Namespaces
aliasing, 179–180. See also using
directive.
in the CLR (Common Language Runtime), 410
dropping. See using
directive.
eliminating. See using
directive.
extern
alias directive, 413
importing types from. See using
directive.
namespaces alias qualifier, 412–413
Naming conventions
class definition, 8
interface guidelines for, 327
local variables, 15
methods, 163
properties, 243
Native code, definition, 26
NDoc tool, 418
delegates, 510
iterators, 676
types, 283
.NET
definition, 895
garbage collection, 418–419, 884–885
.NET Compact Framework compiler, 880
.NET Micro Framework compiler, 880
.NET versions, mapped to C# releases, 29
New line, starting
/n
(newline character), 46, 48, 55
verbatim string literals, 49
WriteLine()
method, 55
new
operator
constructors, 256
NewExpression
, 535
NextId
initialization, 271–273
Non-nullable value types, 478–479
NOT operator, 122
NotImplementedException
, 210
NotOnCanceled
enum, 759
NotOnFaulted
enum, 758
NotOnRanToCompletion
enum, 758
nowarn
option, 157
Nowarn:<warn list>
option, 157
null,
checking for
empty arrays or collections, 666–667
guidelines, 549
null
type
assigning value types to, 64–65
use for, 58
NullReferenceException
invoking delegates, 562
Numbers, formatting as hexadecimal, 43
Numeric conversion, exception handling, 215–216
Object graphs, expression trees as, 535–536
Object initializers
definition, 257
object
members, overriding, 388–395
Object-oriented programming, definition, 218–219
Objects. See also Constructors.
associations, 269
CTS types, 892
destroying, 259
identity vs. equal object values, 388–392
Observer pattern. See Coding the observer pattern with multicast delegates.
Obsolete APIs. See Deprecated APIs.
OfType<T>()
method, 618
One-to-many relationships, 604, 611–613
OnFirstNameChanging()
method, 287
OnLastNameChanging()
method, 287
OnlyOnCanceled
enum, 758
OnlyOnFaulted
enum, 759
OnlyOnRanToCompletion
enum, 759
OnTemperatureChange
event, 566
OnTemperatureChange()
method, 567–568
OnTemperatureChanged()
method, 545–546
Operands, 90
Operational polymorphism, 195
OperationCanceledException
, 772, 806, 811–813
Operator constraints, constraint limitations, 482–483
Operator
keyword, 402
Operator order of precedence, C++ vs. C#, 105
Operator-only statements, C++ vs. C#, 91
Operators. See also specific operators.
arithmetic binary (+
, -
, *
, /
, %)
, 91–92, 95
characters in arithmetic operations, 96
compound assignment (+=
, -=
, *=
, /=
, %=
), 100–101
definition, 89
left-associative, 93
operands, 90
operator-only statements, C++ vs. C#, 91
plus and minus unary (+
, -)
, 90–91
results, 90
right-associative, 93
Operators, increment/decrement (++
, --
)
C++ vs. C#, 105
guidelines, 105
postfix increment operator, 104–105
post-increment operator, 103
prefix increment operator, 104–105
pre-increment operator, 103–104
OR criteria, constraint limitations, 483
Order of operations, 92, 94. See also Precedence.
OrderByDescending()
method, 602
out
, passing parameters, 186–188
out
parameter, properties as values, 252
Out
property, 700
out
vs. pointers, P/Invoke, 853–854
Outer variables, lambda expressions, 528–530
OutOfMemoryException
, 435, 444
Output, passing parameters, 186–188
Overloading. See also Overriding.
==
(equal signs) equality operator, on value types, 362
!=
(exclamation point, equal sign) inequality operator, on value types, 362
cast operator, 402
equality operators on value types, 362
Equals()
method, 362
Overloading, object
members
Equals()
equality operator, 388–395
Overloading, operators
binary operators combined with assignment operators, 397–399
cast operator, 402
conditional logical operators, 400
conversion operators, 401, 403
Overriding. See also Overloading.
abstract members, 317
base classes. See Base classes, overriding.
base classes, virtual methods, 302–307
implicit, C++ vs. C#, 304
methods, 304
properties, 303
PairInitializer<T>
interface, 497
Pair<T>
, 492
Parallel LINQ (PLINQ) queries, multithreading
Parallel loop iterations. See Multithreading, parallel loop iterations.
Parallel programming, definition, 736
Parallel.For()
loops, 807
Parallel.For()
method, 808
Parallel.ForEach()
loops, 807
Parallel.ForEach()
method, 808
ParallelOptions
parameter, 807
Parameter types
determining, 692
explicitly declared, 518
ParameterExpression
, 535
Parameterized types, 461
Parameters
matching caller variables with parameter names, 184
method overloads, 199
names, generating. See Nameof
operator.
names, obtaining, 696
reference types vs. value types, 184–185
specifying by name, example, 199–200
Parameters, passing
Parent type, 220
Parentheses ( () )
grouping operands and operators, 93–94
guidelines, 94
Parse()
method, 69–70, 215–216, 376, 709
Partial methods, C++ vs. C#, 174
Passing
command-line arguments to Main()
method, 180–183
delegates with expression lambdas, 520
by reference, multicast delegates, 558
Passing, parameters
PCLs (portable class libraries), 406
Percent sign, equal (%=) binary/assignment operator, 399
Percent sign (%
) modulo, 91–92, 397–399
Performance
boxing, 365
effects of boxing, 365
locks, 825
TPL (Task Parallel Library), 802–803
Periods (....), download progress indicator, 779
PiCalculator.Calculate()
method, 753
PingButton_Click()
method, 796
Ping.Send()
method, 796
P/Invoke. See also WinRT.
allocating virtual memory, 852
calling external functions, 858–860
declaring external functions, 850–851
declaring types from unmanaged structs, 854–855
description, 850
function pointers map to delegates, 861–862
wrappers for API calls, 860–861
Platform interoperability. See P/Invoke; Unsafe code; WinRT.
Platform portability, managed execution, 28
PLINQ (parallel LINQ) queries, multithreading
Plus sign (+
)
addition operator, overloading, 397–399
arithmetic binary operator, 91–92
with char
type data, 96
concatenating strings, 95
determining distance between two characters, 96
with non-numeric operands, 95
precedence, 92
Plus sign, equal (+=)
binary/assignment operator, 399
Plus signs (++
) increment operator
C++ vs. C#, 105
guidelines, 105
postfix increment operator, 104–105
post-increment operator, 103
prefix increment operator, 104–105
pre-increment operator, 103–104
accessing members of a referent type, 871
allocating data on the call stack, 868–869
dereferencing pointers, 869–871
fixing (pinning) data, 866–868
referent types, 864
unmanaged types, 865
Polymorphism. See also Inheritance; Interfaces.
description, 221
Pop()
method, 456–459, 661–662
Portable class libraries (PCLs), 406
#pragma
preprocessor directive, 153, 156–157
Precedence, 92, 93–94, 160. See also Order of operations.
Precision
binary floating-point types, 97
double
type, 97
Predefined attributes, 709
Predicates
filtering class collections, 591
PreferFairness
enum, 758
Preprocessing, C++ vs. C#, 152
Preprocessor directives. See also Control flow; Flow control.
as comments, 154
as debugging tool, 154
defining preprocessor symbols, 155
disabling/restoring warning messages, 156–157
emitting errors and warnings, 155–156
excluding/including code, 154
handling differences among platforms, 154
specifying line numbers, 157
summary of, 152. See also specific directives.
Preprocessor symbols, defining with preprocessor directives, 155
The Princess Bride, 2
Print()
method, 318
Priority
property, 743
private
access modifier, 235–237, 296–297
private
accessibility modifier, 409
Private
keyword, 237
Private members
definition, 236
Process, definition, 734
Process.Kill()
method, 789
Processor-bound latency, definition, 732
Program
accessing static fields, 267–268
defining properties, 240
Programming, object-oriented definition, 218–219
Programming with dynamic objects
dynamic
directive, 721
dynamic member invocation, 722
dynamic
principles and behaviors, 721–723
dynamic System.Object
, 723–724
implementing a custom dynamic object, 726–728
introduction, 719
invoking reflection with dynamic
, 719–720
reflection, support for extension methods, 723
retrieving member names, 729
signature verification, 722
vs. static compilation, 725–726
Progress update display, 789–791
Projecting collections
definition, 622
with query expressions, 624–627
Properties
automatically implemented, 240–242, 248, 254, 273
automatically implemented, read- only, 280
decorating with attributes, 696–697
definition, 238
naming conventions, 243
overriding, 303
read-only automatically implemen- ted, 280
as ref
or out
parameter values, 252
static, 273
protected
access modifier, 297–298
protected
accessibility modifier, 409
protected internal
accessibility modifier, 409
protected internal
type modifier, 408
Protected members, accessing, 297–298
Pseudoattributes, 719
public
access modifier, 235–237, 407–409
Publishing code, checking for null
, 548
Pulse()
method, 823
Pure virtual functions, C++ vs. C#, 317
Push()
method, 456–459, 661–662
Quantum, definition, 736
Query continuation clauses, 637–638
definition, 621
discarding duplicate members, 639–640
filtering collections, 631–632
flattening a sequence of sequences, 638–639
group
clause, 623
grouping query results, 634–637
into
keyword, 637
projecting collections, 624–627
returning distinct members, 639–640
translating to method calls, 640–641
Query operators. See Standard query operators.
Question mark, colon (?:
) conditional operator, 123–124
Question mark, dot (?.
) null-conditional operator, 125–128
Question mark (?
) nullable modifier, 64–65, 459
Question marks (??
) null-coalescing operator, 124–125, 126
Queue collections, 662
Queue<T>
, 662
Race conditions. See also Thread synchronization.
class collections, 595
threading problems, 738
Ragsdale, S., 26
Rank, arrays
declaring, 72
Reactive Extensions, 816
Readability. See Code readability.
ReadKey()
method, 19
Read-only
automatically implemented properties, 280
fields, encapsulation, 279–281
readonly
modifier
guidelines, 281
ReadToAsync()
method, 779
ReadToEnd()
method, 776
ReadToEndAsync()
method, 779
Recursion. See also Methods, calling.
definition, 192
infinite recursion error, 194
Recursive iterators, 676
ref
parameter, properties as values, 252
ref
type parameters, passing, 185–186
Refactoring
methods, 171
Reference, passing parameters by, 185–186
Reference types
constraints on type parameters, 478–479
copying, 355
vs. value types, 184–185, 353–355
ReferenceEquals()
method, 392
Referencing other assemblies
changing the assembly target, 404
console executables, 404
encapsulation of types, 407–408
internal
access modifiers on type declarations, 407–408
on Mac and Linux, 406
modules, 405
PCLs (portable class libraries), 406
protected internal
type modifier, 408
public
access modifiers on type declarations, 407–408
referencing assemblies, 405–406
referencing assemblies on Mac and Linux, 406
type member accessibility modifiers, 409
Windows executables, 405
Referent types
accessing members of, 871
definition, 864
accessing using System.Type
class, 685
CIL (Common Intermediate Language), 894–895
definition, 684
GetProperties()
method, 685–686
getting an object’s public properties, 685–686
invoking, with dynamic
, 719–720
invoking with dynamic
, 719–720
retrieving Type
objects, 686–687
support for extension methods, 723
uses for, 684
Reflection, on generic types
classes, identifying support for generics, 692–693
determining parameter types, 692
methods, identifying support for generics, 692–693
type parameters for generic classes or methods, 693–694
typeof
operator, 692
__ reftype
keyword, 8
__ refvalue
keyword, 8
#region
preprocessor directive, 153, 158–159
Registering for
notification of task behavior, 760–761
ReleaseHandle()
method, 858
Remove()
method
event internals, 568
removing delegates from chains, 552
removing dictionary elements, 656
System.Delegate
, 649
System.Text.StringBuilder
, 58
RemoveAt()
method, 649
Remove_OnTemperatureChange()
method, 567–568
Removing
delegates from a chain, 550–552
dictionary elements, 656
elements from collections, 649
Replace
, 58
ReRegisterFinalize()
method, 429
Reserved keywords, 6, 8, 679–680
Reset()
method, 584
Resource cleanup. See also Garbage collection.
class collections, 587
exception propagation from constructors, 428
finalization queue, 426
guidelines, 428
with IDisposable
, 424–425, 426–427
introduction, 421
invoking the using
statement, 425–426
resurrecting objects, 429
Result
property, 754
Results, 90
Resurrecting objects, 429
Rethrowing
Retrieving
member names, 729
Type
objects, reflection, 686–687
return
statement, 173
Return values
calling methods, 163
from iterators, 669–671, 673–674
from the ReadLine()
method, 18
Reversing
Right outer join, definition, 604
Right-associative operators, 93
Root references, 418
RunContinuationAsynchronously
enum, 759
Running applications, 3
RunProcessAsync()
method, 788–789
Runtime. See also VES (Virtual Execution System).
circumnavigation encapsulation and access modifiers, 885
CLR (Common Language Runtime), 896
code access security, 886
managed code, definition, 883
managed data, definition, 883
managed execution, definition, 883
reflection, 885
type checking, 885
type safety, 885
RuntimeBinderException
, 722
sbyte
type, 36
calling methods, 167
flow control statements, 116–118
Sealed types constraint limitations, 484
Sealing virtual members, 311–312
Search element not found, 651–652
Searching
Select()
method
projecting class collection data, 592–594
SelectMany()
method
SemaphoreSlim.WaitAsync()
method, 840
Semicolon (;), ending statements, 6–7, 11
Send()
method, 796
SendTaskAsync()
method, 796
SequenceEquals()
method, 618
Sequential invocation, multicast delegates, 552, 554
Serializable objects, 449
Serialization()
method, 714
Serialization-related attributes, 713–714
Serializing
business objects into a database. See Reflection.
set
keyword, 240
SetResult()
method, 789
Setter methods. See Getter/setter methods.
Shared Source CLI compiler, 880
Short circuiting, with the null-coalescing operator, 124–125
SignalAndWait()
method, 835
Signature verification, 722
Simultaneous multithreading, definition, 734
Single backslash character (\
), escape sequence, 46
Single inheritance, 299–301, 343–344
Single-line comments, 24
Single-threaded programs,
definition, 734
Slash, equal (/=
) binary/assignment operator, 399
Smiley face, displaying, 47–48
Sorting
class collections, 601–603. See also Standard query operators, sorting.
Sorting, collections. See also Standard query operators, sorting.
with query expressions, 632–633
Specializing types, 221
SqlException()
method, 448
Square brackets ([ ]), array declaration, 72–74
definition, 353
Stack
, 461
Stackalloc
data, 868
StackOverflowException
, 210
AsParallel()
, 595
caching data, 600
Concat()
, 618
counting elements with Count()
, 595–596
deferred execution, 597–598, 600–601
definition, 588
Distinct()
, 618
filtering with Where()
, 591–592, 597–598
Intersect()
, 618
OfType()
, 618
projecting with Select()
, 592–594
queryable extensions, 619
race conditions, 595
Reverse()
, 618
running LINQ queries in parallel, 594–595
sequence diagram, 599
SequenceEquals()
, 618
System.Linq.Enumerable
method calls, 616–617
table of, 618
Union()
, 618
Standard query operators, join operations
Cartesian products, 608
full outer join, 604
grouping results with GroupBy()
, 610–611
left outer join, 604
many-to-many relationships, 604
one-to-many relationships, 604
one-to-many relationships, with GroupJoin()
, 611–613
outer joins, with GroupJoin()
, 613–614
outer joins, with SelectMany()
, 613–616
right outer join, 604
Start()
method, 305–306, 762–763
State
sharing, class collections, 585
Statements
vs. method calls, 169
multiple, on the same line, 11–12, 13
splitting across multiple lines, 11, 12
STAThreadAttribute
, 846
compilation vs. programming with dynamic objects, 725–726
fields, 267–268. See also Instance fields; static
keyword.
properties, 273
static
keyword, 266. See also Static, fields.
Status
property, 754
Storage
disk, 662
reclaiming. See Finalizers; Garbage collection.
String interpolation, formatting with, 20
string()
method, 48
String methods
instance methods, 51
static methods, 51–52. See also using
directive; using static
directive.
string.Format
method, 51
string.join
statement, 801
Strings
“ “ (double quotes), coding string literals, 48
@ (at sign), coding verbatim strings, 48
$
(dollar sign), string interpolation, 48
$@
(dollar sign, at sign), string interpolation, 50
concatenation at compile time, C++ language, vs. C#, 50
concatenation at compile time, vs. C++, 50
formatting, 54
having no value vs. empty, 59
literals, 48
read-only properties, 55
representing a binary display, 132
type for, 48
verbatim string literals, 49
Strong references, garbage collection, 420–421
struct
keyword
vs. class
, 677
declaring a struct, 356
StructLayoutAttribute
, 854–855
declaring, 356
definition, 356
finalizer support, 360
referential identity, 360
Structural equality, delegates, 526–527
Subtypes, 220
Sum()
method, 618
Super types, 220
SuppressFinalize()
method, 426
Surrogate pairs, 46
Swap()
method, 186
Swapping array data elements, 79
switch
statements
catching exceptions, 436
fall-through, C++ vs. C#, 145
syntax, 110
Switching overhead, 737
Synchronization. See Thread synchronization.
Synchronization context, 793–795
Synchronization types. See Thread synchronization, synchronization types.
Synchronized()
method, 827
Synchronizing
multiple threads with Monitor
class, 821
Synchronous delegates, 751
Synchronous high-latency operations, 775–777
Syntax
immutable strings, 17
System
, 165
System.Action
delegates, 524–525
System.ApplicationException
, 210, 435, 436
System.ArgumentException
, 210, 433, 436
System.ArgumentNullException
, 210
System.ArithmeticException
, 210
System.Array
, 484
System.Array.Reverse()
method, 85–86
System.ArrayTypeMismatchException
, 210
System.AttributeUsageAttribute
, 706–709
System.Collection.Generic.List<T>.FindAll()
method, 652–653
System.Collections
, 165
System.Collections.Generic.IEnumerable<T>
. See IEnumerable<T>
interface.
System.Collections.Generics
, 165
System.Collections.Generic.Stack<T>
, 464, 584–587
System.Collections.IEnumerable
. See IEnumerable
interface.
System.Collection.Stack.Pop()
method, 456–459
System.Collection.Stack.Push()
method, 456–459
System.ComponentModel.Win32Exception
method, 855–856, 862
System.Console.Clear()
method, 154
System.Console.Read()
method, 19
System.Console.ReadKey()
method, 19
System.Console.ReadLine()
method
reading from the console, 18–19
return values, 168
System.Console.Write()
method
return values, 168
System.Console.WriteLine()
method
outputting a blank line, 55
overriding ToString()
method, 384–385
return values, 168
System.Data
, 165
System.Data.SqlClient.SqlException()
, 448
System.Delegate
constraint limitations, 484
multicast delegate internals, 554
System.Delegate.Combine()
method
combining delegates, 552
constraint limitations, 484
event internals, 568
System.Delegate.Remove()
method
event internals, 568
removing delegates from chains, 552
removing list elements, 649
System.Diagnostics.ConditionalAttribute
, 710–711
System.Diagnostics.Processor
, 851
System.Diagnostics.Trace.Write()
method, 384
System.Drawing
, 165
System.Dynamic.DynamicObject
, 728
System.Dynamic.IDynamicMetaObject Provider
interface, 726–729
System.Enum
, 484
System.Enum.IsDefined()
method, 379
System.Enum.Parse()
method, 376
System.Environment.CommandLine
, 10
System.Environment.FailFast()
method, 435, 436
System.Environment.Newline
method, 55
System.EventHandler<T>
, 566
System.Exception
, 209–210, 435, 436
System.ExecutionEngineException
, 435
System.FormatException
, 210
System.Func
delegates, 524–525
System.GC
, 419
System.GC.SuppressFinalize()
method, 426
System.IndexOutOfRangeException
, 210
System.IntPtr
, 852
System.InvalidCastException
, 210
System.InvalidOperationException
, 210, 438
System.IO
, 165
System.IO.FileAttributes
, 377
System.Linq
, 165
System.Linq.Enumerable
aggregate functions, 618
Average()
method, 618
Count()
method, 618
GroupBy()
method, grouping results, 610–611
Max()
method, 618
Min()
method, 618
Sum()
method, 618
System.MulticastDelegate
, 484, 513–516
System.NonSerializable
, 714–715
System.NotImplementedException
, 210
System.Nullable<T>
, 461
System.NullReferenceException
, 210
System.ObsoleteAttribute
, 712
System.OutOfMemoryException
, 435
System.Reflection.MethodInfo
property, 513
System.Runtime.CompilerServices.CallSite<T>
, 723–724
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Catch()
method, 439
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
method, 439–440
System.Runtime.InteropServices.COMException
, 435
System.Runtime.InteropServices.SafeHandle
, 858
System.Runtime.InteropServices.SEHException
, 435
System.Runtime.Serialization.ISerializable
, 715–716
System.Runtime.Serialization.OptionalFieldAttribute
, 718
System.Runtime.Serialization.SerializationInfo
, 715–716
System.Runtime.Serialization.StreamingContext
, 715–716
System.Security.AccessControl.MutexSecurity
objects, 834
System.SerializableAttribute
, 713–714, 718–719
System.ServiceModel
, 166
System.StackOverflowException
, 210, 435
System.String.string
method, 48
System.SystemException
, 435, 436
System.Text
, 165
System.Text.RegularExpressions
, 165
System.Text.StringBuilder
, 58, 477
System.Text.StringBuilder.Remove()
method, 58
System.Threading
, 165
System.Threading.AutoResetEventSlim
, 836–839
System.Threading.Interlocked
Add()
method, 829
CompareExchange()
method, 828–829
CompareExchange<T>
method, 829
Decrement()
method, 829
Exchange<T>
method, 829
Increment()
method, 829
Read()
method, 829
System.Threading.Interlocked
methods, 828–829
System.Threading.ManualResetEvent
, 836–839
System.Threading.ManualResetEventSlim
, 836–839
System.Threading.Monitor
, 821
System.Threading.Monitor.Enter()
method, 366, 821, 823, 825–826
System.Threading.Monitor.Exit()
method, 366, 823, 825–826
System.Threading.Monitor.Pulse()
method, 823
System.Threading.Mutex
, 833–835
System.Threading.Tasks
, 165
System.Threading.Thread
, 734. See also Multithreading, with System.Threading.Thread
.
System.Threading.Timer
, 846
System.Threading.WaitHandle
, 835
System.Timers.Timer
, 846
System.Type
class, accessing metadata, 685
System.UnauthorizedAccessException
, 449
System.WeakReference
, 420
System.Web
, 165
System.Windows
, 166
System.Windows.Forms
, 166
System.Windows.Forms.Timer
, 846
System.Windows.Threading.DispatcherTimer
, 846
System.Xml
, 166
TAP (Task-based Asynchronous Pattern). See Multithreading, task-based asynchronous pattern.
Target property, 513
Task Parallel Library (TPL). See TPL (Task Parallel Library).
Task schedulers, 750–751, 793–795
Task-based asynchronous pattern. See Multithreading, task-based asynchronous pattern.
Task-based Asynchronous Pattern (TAP). See Multithreading, task-based asynchronous pattern.
TaskCanceledException
, 772
TaskCompletionSource.SetResult()
method, 789
TaskCompletionSource<T>
object, 788–789
TaskContinuationOptions
enums, 758–759
Task.ContinueWith()
method, 756, 793–794, 798
TaskCreationOptions.LongRunning
option, 773–774
Task.Delay()
method, 745, 845–846
Task.Factory.StartNew()
method, 772–773
Tasks
antecedent, 757
associating data with, 755
asynchronous. See Multithreading, asynchronous tasks.
canceling. See Multithreading, canceling tasks.
chaining, 757
cold, 752
composing large from smaller, 756–758
vs. delegates, 751
disposable, canceling, 774
drawbacks, 775
hot, 752
identification, 755
long-running, canceling, 773–774
registering for notification of behavior, 760–761
status, getting, 754
TaskScheduler
, 793
Temporary storage pool. See Stack.
Ternary operators, definition, 123
TextNumberParser.Parse()
method, 434
TextToUpper()
method, 57
ThenByDescending()
method, 602
this
keyword
definition, 228
identifying field owner, 228–229
passing in a method call, 231–232
Thread pool, definition, 735
Thread pooling, definition, 747–749
Thread safe code, definition, 734
Thread safe delegate invocation, 550
Thread safety
definition, 819
delegates, 550
Thread synchronization. See also Deadlocks; Race conditions.
with no await
operator, 820
Thread synchronization, synchronization types
concurrent collection classes, 840–841
Thread synchronization, thread local storage
definition, 841
ThreadStaticAttribute
, 843–845
Thread synchronization, uses for
atomicity of reading and writing to variables, 819
declaring fields as volatile
, 828
event notification with multiple threads, 830
lock
objects, 825
lock performance, 825
locking guidelines, 827
locking on this
, typeof
, and string
, 826–827
with MethodImplAttribute
, 827
with MethodImplOptions.Synchronized()
method, 827
multiple threads and local variables, 819–820
sample pseudocode execution, 818
synchronizing local variables, 819–820
synchronizing multiple threads with Monitor
class, 821
with System.Threading.Interlocked
methods, 828–829
thread safety, definition, 819
thread-safe event notification, 831
torn read, definition, 819
unsynchronized local variables, 820
unsynchronized state, 817
volatile
keyword, 828
Threading model, definition, 734
Threads
checking for life, 744
definition, 734
foreground vs. background, 743
putting to sleep, 744
reprioritizing, 743
waiting for, 743
Thread-safe event notification, 831
Thread.Sleep()
method, putting threads to sleep, 744
ThreadState
property, 744
ThreadStaticAttribute
, 843–845
Three-dimensional arrays, 77–78
ThrowIfCancellationRequested()
method, 772
Throwing exceptions. See also Catching exceptions; Exception handling.
ArgumentNullException
, 436
ArgumentOutOfRangeException
, 436
checked and unchecked conversions, 451–453
code sample, 434
guidelines, 450
identifying the parameter name, 435
NullReferenceException
, 436
rethrowing wrapped exceptions, 449–453
without replacing stack information, 439
TicTacToe game. See also Arrays.
checking player input, 142–143
conditional operators, 123
determining remaining moves, 141
#endregion
preprocessor directive, example, 158–159
if/else
example, 111
initializing, 73
nested if
statements, 112
#region
preprocessor directive, example, 158–159
tracking player moves, 147–148
Tilde (~
) bitwise complement operator, 134
Time slice, definition, 736
Time slicing, definition, 736
Time slicing costs, 737
Timers. See Thread synchronization, timers.
TKey
parameter, 479
ToArray()
method, 600
ToDictionary()
method, 600
ToList()
method, 600
ToLookup()
method, 600
Torn read, definition, 819
ToString()
method, 69–70, 384–385, 709
TPL (Task Parallel Library). See also Multithreading, parallel loop iterations.
asynchronous high-latency operations, 777–781
Trapping errors, 203–209. See also Exception handling.
TrimToSize()
method, 662
True/false evaluation. See Boolean expressions.
TryGetMember()
method, 728
TryGetPhoneButton()
method, 186–188
TryParse()
method, 70–71, 215–216, 690–692
TryParse<T>()
method, 376
TrySetMember()
method, 728
Tuple.Create()
method, 471–472
TValue
parameter, 479
Two-dimensional arrays, 74, 79
Type
array defaults, 73
conversion, programming with dynamic objects, 721–722
incompatibilities, anonymous types, 576–577
inference, generic methods, 487–489
name qualifier, calling methods, 166–167
Type categories, reference types
definition, 61
memory area of the referenced data, 63
Type categories, value types
? (question mark), nullable modifier, 64–65
definition, 61
description, 62
Type definition
casing, 8
naming conventions, 8
Type
objects, retrieving, 686–687
Type parameter list, declaring methods, 172
Type parameters
constraints on. See Constraints on type parameters.
generic classes, 464
for generic classes or methods, 693–694
Type safety
covariance, 498
managed execution, 27
programming with dynamic objects, 720–721, 726
Type.ContainsGenericParameters
property, 693
typeof
keyword, locking, 826–827
aliasing, 179–180. See also using
directive.
bool
(Boolean), 45
data conversion with the as
operator, 322–323
declaring on the fly. See Anonymous types.
definition, 15
extending. See Inheritance.
implicitly typed local variables, 60–61
integral, 96
string
, 48
for strings, 48
underlying, determining, 321–322
well formed. See Well-formed types.
casting, 65
checked block example, 67
defining custom conversions, 295–296
implicit conversion, 65, 68–69
numeric to Boolean, 68
overflowing an integer value, 66–68
unchecked block example, 67–68
Types, fundamental numeric. See also Literal values.
byte
, 36
C# vs. C++ short
type, 37
floating-point types. See Floating-point types.
formatting numbers as hexadecimal, 43
int
, 36
int
(integer), 14
integer literals, determining type of, 41–42
keywords associated with, 36
long
, 36
sbyte
, 36
short
, 36
uint
, 36
ulong
, 36
ushort
, 36
uint
type, 36
ulong
type, 36
UML (Unified Modeling Language), 345
Unary operators
definition, 122
UnaryExpression
, 535
UnauthorizedAccessException
, 449, 804
Unchecked block example, 67–68
#undef
preprocessor directive, 153, 155
Underscore (_
)
in identifier names, 7
line continuation character, 11
in variable names, 15
Underscores (__
), in keyword names, 8
Undo, with a generic Stack
, 461
Undo()
method, 458
Unexpected inequality, float
type, 97–100
Unhandled exceptions
handling with AggregateException
, 762–765
UnhandledException
event, 766
Unified Modeling Language (UML), 345
Union()
method, 618
Unmanaged code, definition, 26
Unmanaged types, 865
Unmodifiable. See Immutable.
Unsafe code. See also P/Invoke; Pointers and addresses; WinRT.
executing by delegate, 872–873. See also P/Invoke; WinRT.
unsafe
modifier, 863
unsafe
statement, 863
/unsafe
switch, 864
Unsynchronized local variables, 820
Unsynchronized state, 817
Unwrap()
method, 779
ushort
type, 36
dropping namespaces, 53–54, 175–177
importing types from namespaces, 175–177
nested namespaces, 176
wildcards, Java vs. C#, 176
using
statement
deterministic finalization, 423–426
abbreviating a type name, 178–179
Validating properties, 244–246
Value, passing parameters by, 183–184
value
keyword, 240
to an implemented interface. See Boxing.
to its root base class. See Boxing.
Value types
custom types. See Enums; Structs.
immutability, 357
vs. reference types, 184–185, 353–355
temporary storage pool. See stack.
Values
CTS types, 892
var
keyword
C++ vs. C#, 575
implicitly typed local variables, 60–61
JavaScript vs. C#, 575
Visual Basic vs. C#, 575
Variables. See also Local variables.
setting to null
, 58
type, 14
using, 17
Variables, global, C++ vs. C#, 266
Variant
C++ vs. C#, 575
JavaScript vs. C#, 575
Visual Basic vs. C#, 575
VerifyCredentials()
method, 344
Versioning interfaces, 346–347
Vertical bar, equal sign (|=
) compound assignment operator, 133–134
Vertical bar (|
) OR operator, 131, 132, 397–399
Vertical bars (||
) OR operator, 121
VES (Virtual Execution System). See also CIL (Common Intermediate Language); CLI (Common Language Infrastructure); Runtime.
managed execution, 26
Virtual abstract members, 317
Virtual Execution System (VES). See VES (Virtual Execution System).
Virtual fields, properties as, 249–250
Virtual functions, pure, 317
Virtual members, sealing, 311–312
Virtual memory, allocating with P/Invoke, 852
Virtual methods
custom dynamic objects, 728
Java vs. C#, 440
overriding base classes, 302–307
VirtualMemoryManager
, 851
VirtualMemoryPtr
, 858
Visual Basic vs. C#
importing namespaces, 176
line-based statements, 11
this
keyword, 230
var
keyword, 575
Variant, 575
void*
, 575
void
type, 59
void*
C++ vs. C#, 575
JavaScript vs. C#, 575
Visual Basic vs. C#, 575
void
type
C++ vs. C#, 59
no value vs. empty string, 59
in partial methods, 287
use for, 58
volatile
keyword, 828
WaitAll()
method, 835
WaitAny()
method, 835
WaitAsync()
method, 840
WaitForExit()
method, 788
WaitHandle
, 774
Warning messages, disabling/restoring, 156–157
#warning
preprocessor directive, 153, 155–156
Weak references, garbage collection, 420–421
WebRequest.GetResponseAsync()
method, 779
determining whether two objects are equal, 392–395
implementing Equals()
equality operator, 392–395
object identity vs. equal object values, 388–392
overriding Equals()
equality operator, 392–395
Well-formed types, garbage collection. See also Resource cleanup.
Collect()
method, 419
introduction, 418
root references, 418
Well-formed types, namespaces
in the CLR (Common Language Runtime), 410
extern
alias directive, 413
guidelines, 412
namespaces alias qualifier, 412–413
naming conventions, 410
nesting, 411
Well-formed types, overloading object
members
Equals()
equality operator, 388–395
Well-formed types, overloading operators
binary operators combined with assignment operators, 397–399
cast operator, 402
conditional logical operators, 400
conversion operators, 401, 403
Well-formed types, referencing other assemblies
changing the assembly target, 404
console executables, 404
encapsulation of types, 407–408
internal
access modifiers on type declarations, 407–408
modules, 405
PCLs (portable class libraries), 406
protected internal
type modifier, 408
public
access modifiers on type declarations, 407–408
referencing assemblies, 405–406
referencing assemblies on Mac and Linux, 406
type member accessibility modifiers, 409. See also specific modifiers.
Windows executables, 405
Well-formed types, resource cleanup
exception propagation from constructors, 428
finalization queue, 426
guidelines, 428
with IDisposable
, 424–425, 426–427
introduction, 421
invoking the using
statement, 425–426
resurrecting objects, 429
Well-formed types, XML comments
associating with programming constructs, 414–416
generating an XML documentation file, 416–418
guidelines, 418
when
clauses, catching exceptions, 438
where
clauses, 623–624, 631–632
Where()
method, 591–592, 597–598
while
loops, 108, 134–135, 136–137
While()
method, 584
while
statement, 108, 134–135, 136–137
Win32, error handling in P/Invoke, 855–857
Windows Desktop CLR compiler, 880
Windows Runtime. See WinRT.
WinRT. See also P/Invoke.
automatically shimmed interfaces, 875–876
definition, 896
task-based asynchrony, 876
Wrappers for API calls from P/Invoke, 860–861
Write()
method
Write-only properties, 247–248
WriteWebRequestSizeAsync()
method, 780, 783
XML (Extensible Markup Language), 25, 416–418
XML comments
associating with programming constructs, 414–416
delimited comments, 24
generating an XML documentation file, 416–418
guidelines, 418
single-line, 24
yield break
statement, 677–678
yield
keyword, 6
yield return
statement
implementing BinaryTree<T>
, 673–676
requirements, 681
returning iterator values, 670–671
yield return
statements, 6
ZipCompression
, 337