-
(minus sign)
arithmetic subtraction operator, 91–92
precedence, 92
subtraction operator, overloading, 397–399
()
(parentheses)
grouping operands and operators, 93–94
guidelines, 94
_
(underscore)
in identifier names, 7
line continuation character, 11
in variable names, 15
{ }
(curly braces)
formatting code, 13
omitting, 116
as string literals, 54
@
(at sign)
coding verbatim strings, 48
inserting literal backslashes, 49
keyword prefix, 8
+
(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
-= (minus sign, equal)
binary/assignment operator, 399
-
(hyphens), in identifier names, 7
__
(two underscores), in keyword names, 8
; (semicolon), ending statements, 6–7, 11
.... (periods), download progress indicator, 779
" "
(double quotes), coding string literals, 48
[ ] (square brackets), array declaration, 72–74
(backslashes), as literals, 49
$
(dollar sign), string interpolation, 48
$@
(dollar sign, at sign), string interpolation, 50
< >
(angle brackets), in XML, 25
&
(ampersand) AND operator, 131, 132
&&
(ampersands) AND operator, 121
=
(equal sign) assignment operator, 16, 118
==
(equality operator), C++ vs. C#, 118
assigning variables, 16
definition, 16
precedence, 92
... (ellipsis) binary/assignment operator, 399
*= (asterisk, equal sign) binary/assignment operator, 399
%= (percent sign, equal) binary/assignment operator, 399
/=
(slash, equal) binary/assignment operator, 399
&=
(ampersand, equal sign) compound assignment operator, 133–134
^=
(caret, equal sign) compound assignment operator, 133–134
|=
(vertical bar, equal sign) compound assignment operator, 133–134
?:
(question mark, colon) conditional operator, 123–124
--
(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
/
(forward slash) division operator
precedence, 92
.
(dot) dot operator, 126, 871
==
(equal signs) equality operator
in place of = (equal sign) assignment operator, 119–120
/ (forward slash) in XML, 25
++
(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
!=
(exclamation point, equal sign) inequality operator
testing for inequality, 119–120
<
(less than sign) less than operator, 120, 396–397
<=
(less than, equal sign) less than or equal operator, 120, 396–397
!
(exclamation point) logical NOT operator, 122
%
(percent sign) modulo, 91–92, 397–399
*
(asterisk) multiplication operator, 91–92, 397–399
?
(question mark) nullable modifier, 64–65, 459
??
(question marks) null-coalescing operator, 124–125, 126
?.
(question mark, dot) null-conditional operator, 125–128
|
(vertical bar) OR operator, 131, 132, 397–399
||
(vertical bars) OR operator, 121
<<=
(less than signs, equal) shift left assignment operator, 130
<<
(less than signs) shift left operator, 130, 397–399
^
(caret) XOR operator, 122, 131, 397–399
\
(single backslash character), escape sequence, 46
>
(greater than sign), greater than operator, 120, 396–397
>=
(greater than, equal sign), greater than or equal operator, 120, 396–397
=>
(equal sign, greater than) lambda operator, 517, 524
>>
(greater than signs), shift right operator, 130, 397–399
>>=
(greater than signs, equal ) shift right assignment operator, 130
~
(tilde) bitwise complement operator, 134
Abstract classes. See also Derivation.
definition, 314
derived from System.Object
, 320–321
vs. interfaces, 338
definition, 314
“is a” relationships, 317
overriding, 317
virtual, 317
Access modifiers. See also Encapsulation.
on getters and setters, 251–252
purpose of, 236
Add()
method
appending items to lists, 648
inserting dictionary elements, 654–655
System.Threading.Interlocked
class, 829
thread synchronization, 829–830
Addresses. See Pointers and addresses.
Aggregate functions, 618
AggregateException
, 557–558, 762–765
AggregateException.Flatten()
method, 780
AggregateException.Handle()
method, 764, 780
Aggregation
multiple inheritance, interfaces, 343–344
Aliasing, namespaces, 179–180. See also using
directive.
AllocExecutionBlock()
method, 857
AllowMultiple
member, 707
Alternative flow control statements, 111
Ampersand, equal sign (&=
) compound assignment operator, 133–134
Ampersand (&
) AND operator, 131, 132
Ampersands (&&
) AND operator, 121
Angle brackets (< >
), in XML, 25
Anonymous functions
guidelines, 533
Anonymous methods. See also Lambda expressions.
definition, 522
guidelines, 523
parameterless, 523
explicit local variables, 263–265
generating, 578
implicit local variables, 572–576
type incompatibilities, 576–577
Antecedent tasks, 757
Apartment-threading models, 846
APIs (application programming interfaces)
calls from P/Invoke, wrappers, 860–861
definition, 25
deprecated, 712
as frameworks, 26
Append
method, 58
AppendFormat
method, 58
Appending items to collections, 648
Applicable method calls, 201
Applications, compiling, 3
__ arglist
keyword, 8
ArgumentNullException
, 434–435, 436
ArgumentOutOfRangeException
, 435, 436
Arguments
named, calling methods, 199
Arity (number of type parameters), 471–472
Array declaration
Array types, constraint limitations, 484
Arrays. See also Collections; Lists; TicTacToe game.
accessing elements of, 73, 78–79
of arrays, 78
converting collections to, 646
designating individual items, 71
exceeding the bounds of, 80–81
Length
member, 80
number of dimensions, 72
number of items, getting, 80–81
size, specifying, 75
swapping data elements, 79
two-dimensional, 74, 79. See also TicTacToe game.
type defaults, 73
AsParallel()
method, 595
AspNetSynchronizationContext
, 794
Assert()
method, 97
Associativity of operators, 92, 93–94
Asterisk, equal sign (*=) binary/assignment operator, 399
Asterisk (*
) multiplication operator, 91–92, 397–399
async
keyword
purpose of, 786
task-based asynchronous pattern, 781–786
in WinRT, 876
Asynchronous continuations, 756–762
Asynchronous delays, 745
Asynchronous high-latency operations with the TPL, 777–781
Asynchronous operations, 736, 741–743
Asynchronous tasks. See Multithreading, asynchronous tasks.
AsyncState
, 755
At sign (@
)
coding verbatim strings, 48
inserting literal backslashes, 49
keyword prefix, 8
Atomic operations, threading problems, 738
Atomicity of reading and writing to variables, 819
AttachedToParent
enum, 758
Attributes
adding metadata about assemblies, 697–698
alias command-line options, 701
AllowMultiple
member, 707
decorating properties with, 696–697
definition, 683
deserializing objects, 714–715
duplicate names, 707
guidelines, 699, 700, 705, 707
initializing with a constructor, 701–705
vs. interfaces, 349
named parameters, 707
Parse()
method, 709
predefined, 709
pseudoattributes, 719
serialization-related, 713–714
setting bits or fields in metadata tables. See Pseudoattribute.
uses for, 696
warning about deprecated APIs, 712
Automatically implemented properties
initializing, 242
internals, 254
NextId
implementation, 273
Average()
method, 618
task-based asynchronous pattern, 781–786
await
operator
multithreading with System.Threading.Thread
class, 745
in WinRT, 876
Backslashes (), as literals, 49
Base classes, inheritance, 302
Base classes, overriding. See also Derivation.
accessing a base member, 312–313
base
keyword, 313
introduction, 302
sealing virtual members, 311–312
base
keyword, 313
Base members, accessing, 312–313
Base type, 220
BCL (Base Class Library), 27, 28, 893, 895
Binary digits, definition, 128
Binary display, string representation of, 132
Binary floating-point types, precision, 97
Binary search of arrays, 81–83
BinaryExpression
, 535
BinarySearch()
method
bitwise complement of, 652
BinaryTree<T>
, 476–477, 670–671
Bits, definition, 128
Bitwise complement of BinarySearch()
method, 652
<<
(less than signs), shift left operator, 130
<<=
(less than, equal signs), shift left assignment operator, 130
&
(ampersand) AND operator, 131, 132, 378
&=
(ampersand, equal sign) compound assignment operator, 133–134
^=
(caret, equal sign) compound assignment operator, 133–134
|=
(vertical line, equal sign) compound assignment operator, 133–134
|
(vertical bar) OR operator, 131, 132, 378
^
(caret) XOR operator, 131
>>
(greater than signs), shift right operator, 130
>>=
(greater than, equal signs), shift right assignment operator, 130
~
(tilde) bitwise complement operator, 134
binary digits, definition, 128
bits, definition, 128
bytes, definition, 128
masks, 132
multiplication and division with bit shifting, 130
string representation of a binary display, 132
Block statements. See Code blocks.
BlockingCollection<T>
, 840
bool
(Boolean) types
description, 45
returning from lambda expres- sions, 520
Boolean expressions. See also Bitwise operators.
<
(less than sign), less than operator, 120
<=
(less than, equal sign), less than or equal operator, 120
==
(equal signs) equality operator, 119–120
!=
(exclamation point, equal sign) inequality operator, 119–120
>
(greater than sign), greater than operator, 120
>=
(greater than, equal sign), greater than or equal operator, 120
evaluating. See if
statements.
example, 118
Boolean expressions, logical operators
!
(exclamation point), logical negation operator, 122
?:
(question mark, colon), conditional operator, 123–124
?.
(question mark, dot), null-conditional operator, 125–128
??
(question marks), null-coalescing operator, 124–125, 126
&&
(ampersands), AND operator, 121
^
(caret), XOR operator, 122
||
(vertical lines), OR operator, 121
.
(dot) dot operator, 126
introduction, 120
Boolean values, replacing with enums, 372
avoiding during method calls, 369–370
InvalidCastException
, 366
performance, 365
value types in the lock
statement, 366–368
Break()
method, 808
Breaking parallel loop iterations, 808
byte
type, 36
Bytes, definition, 128
C language
pointer declaration, vs. C#, 865
similarities to C#, 2
C# language
case sensitivity, 2
compiler, 3
definition, 895
C++ language, similarities to C#, 2
C++ language vs. C#
=
(assignment operator) vs. ==
(equality operator), 118
array declaration, 72
delete
operator, 224
deterministic destruction, 427, 884
explicit deterministic resource cleanup, 224
garbage collection, 884
global methods, 171
global variables and functions, 266
header files, 174
implicit deterministic resource cleanup, 224
implicit nondeterministic resource cleanup, 224
implicit overriding, 304
increment/decrement operators, 105
local variable scope, 118
main()
method, 10
method calls during construction, 307
multiple inheritance, 299
operator order of precedence, 105
operator-only statements, 91
order of operations, 94
partial methods, 174
pointer declaration, 865
preprocessing, 152
pure virtual functions, 317
string concatenation at compile time, 50
switch statement fall-through, 145
var
keyword, 575
Variant, 575
void*
, 575
void
type, 59
Caching data in class collections, 600
Calculate()
method, 753
Caller, 163
Calling
constructors, 255–256, 261–262
methods. See Methods, calling.
CamelCase, 7
Canceling
parallel loop iterations, 805–806
tasks. See Multithreading, canceling tasks.
CancellationTokenSource
, 770
CancellationTokenSource.Cancel()
method, 770–771
Capacity()
method, 647
Capturing loop variables, 531–533
Caret, equal sign (^=
) compound assignment operator, 133–134
Caret (^
) XOR operator, 122, 131, 397–399
Cartesian products, 608, 638–639
Casing
local variables, 15
Cast operator
overloading, 402
Casting
between arrays of enums, 375
between base and derived types, 293–294
definition, 65
implicit base type casting, 293–294
with inheritance chains, 294–295
inside generic methods, 490–491
type conversion without, 69–70
Catch blocks
catching different exception types, 436–437
Catch
clause, 762
Catch()
method, 439
conditional clauses, 438
description, 436
different exception types, 436–437
exception conditions, 438
rethrowing existing exceptions, 438–439
switch
statements, 436
when
clauses, 438
Central processing unit (CPU), definition, 734
Chaining
multicast delegates, 555
tasks, 757
char
(character) types, 14, 45
Checked block example, 67
Checking for null
guidelines, 549
Child type, 220
CIL (Common Intermediate Language). See also CLI (Common Language Infrastructure).
compiling C# source code into, 26, 891–892
compiling into machine code, 878
definition, 895
disassembling, tools for, 33. See also ILDASM
.
CIL disassembler. See ILDASM
.
Class collections. See also IEnumerable
interface; IEnumerable<T>
interface.
cleaning up after iteration, 586–587
error handling, 587
iterating over using while()
, 584
resource cleanup, 587
sharing state, 585
Class collections, foreach
loops
with arrays, 385
code example, 586
with IEnumerable
interface, 587
with IEnumerable<T>
interface, 583–585
modifying collections during, 587–588
Class collections, sorting. See also Standard query operators, sorting.
ascending order ThenBy()
, 601–603
ascending order with OrderBy()
, 601–603
descending order with OrderByDescending()
, 602
descending order with ThenByDescending()
, 602
Class definition
definition, 8
guidelines, 8
naming conventions, 8
Class libraries
definition, 404
Class members, definition, 224
Class type
combining with class
or struct
, 482
class
vs. struct
, 677
Classes
abstract. See Abstract classes.
adding instance methods, 276
association with methods, 163
base. See Base classes.
within classes. See Nested, classes.
derived. See Derivation.
fields, 225
guidelines, 222
identifying support for generics, 692–693
member variables, 225
polymorphism, 221
private members, 236
spanning multiple files, Java vs. C#, 4. See also Partial methods.
splitting across multiple files. See Partial methods.
CLI (Common Language Infrastructure). See also CIL (Common Intermediate Language); VES (Virtual Execution System).
application domains, 888
contents of, 879
CTS (Common Type System), 892
implementations, 879
objects, 892
values, 892
xcopy deployment, 891
Closed over variables, 528–530
Closures, 531
CLR (Common Language Runtime), 896. See also Runtime.
CLS (Common Language Specification)
BCL (Base Class Library), 893
definition, 896
description, 893
FCL (Framework Class Library), 893
managed execution, 27
Code access security, 27
vs. brevity, 169
indenting with whitespace, 12–13
Coding the observer pattern with multicast delegates
connecting publisher with subscribers, 546–547
defining subscriber methods, 544–545
defining the publisher, 545–546
delegate operators, 550–552. See also specific operators.
getting a list of subscribers, 557–558
guidelines, checking for null
, 549
invoking a delegate, 547
method returns, 558
multicast delegate internals, 554
new delegate instances, 550
passing by reference, 558
removing delegates from a chain, 550–552
sequential invocation, 552, 554
thread safe delegate invocation, 550
Cold tasks, 752
Collect()
method, 419
Collection classes
dictionary collections, 653–657
linked list collections, 663
queue collections, 662
Collection initializers
definition, 578
initializing anonymous type arrays, 579–582
initializing collections, 579
Collection interfaces, customizing
appending items to, 648
comparing dictionary keys, 658–659
converting to arrays, 646
counting collection elements, 646
dictionary class vs. list, 644–646
finding even elements, 653
finding multiple items, 652–653
generic hierarchy, 645
inserting new elements, 651–652
lists vs. dictionaries, 644–645
order of elements, 657
removing elements, 649
search element not found, 651–652
searching collections, 651–653
specifying an indexer, 644–646
Collection interfaces with standard query operators
caching data, 600
counting elements with Count()
, 595–596
deferred execution, 597–598, 600–601
definition, 588
filtering with Where()
, 591–592, 597–598
guidelines, 596
projecting with Select()
, 592–594
queryable extensions, 619
race conditions, 595
running LINQ queries in parallel, 594–595
sequence diagram, 599
table of, 618
Collections. See also Anonymous types; Arrays; Class collections; Lists.
discarding duplicate members, 639–640
filtering, 622
projecting, 622
returning distinct members, 639–640
accessing elements without modifying the stack, 661–662
appending items to, 648
counting elements of, 646
FIFO (first in, first out), 662
finding even elements, 653
finding multiple items, 652–653
inserting new elements, 651–652, 661–662
LIFO (last in, first out), 661
order of elements, 657
removing elements, 649
requirements for equality comparisons, 659–660
search element not found, 651–652
Collections, sorting. See also Standard query operators, sorting.
with query expressions, 632–633
COM threading model, controlling, 846
Combine()
method
combining delegates, 552
constraint limitations, 484
event internals, 568
vs. Swap()
method, 186
CommandLineAliasAttribute
, 701–702
CommandLineSwitchRequiredAttribute
, 699–701
Comments
vs. clear code, 24
delimited, 24
guidelines, 24
multi-line, 154
preprocessor directives as, 154
single-line, 24
types of, 24
XML delimited, 24
XML single-line, 24
Common Intermediate Language (CIL). See CIL (Common Intermediate Language).
Common Language Infrastructure (CLI). See CLI (Common Language Infrastructure).
The Common Language Infrastructure Annotated Standard, 26
Common Language Runtime. See Runtime.
Common Language Runtime (CLR), 896. See also Runtime.
Common Language Specification (CLS). See CLS (Common Language Specification).
Common Type System (CTS), 27, 892, 896
CompareExchange()
method, 828–829
CompareExchange<T>
method, 829
CompareTo()
method, 477, 649–650
Comparing
for equality, float
type, 97–100
ComparisonHandler
delegate, 509, 510–512, 514–515
Compatible method calls, 201
Compile()
method, 535
C# language, 3
CLI (Common Language Infrastructure), 879–882
CoreCLR compiler, 880
csc.exe
compiler, 3
DotGNU Portable NET compiler, 880
JIT (just-in-tme) compiler, 881
mcs.exe
compiler, 3
Microsoft Silverlight compiler, 880
Mono, 3
.NET Compact Framework, 880
.NET Micro Framework, 880
Shared Source CLI, 880
Windows Desktop CLR, 880
applications, 3
C# source code into CIL, 891–892
into CIL, 26
jitting, 881
with the Mono compiler, 3
NGEN tool, 881
Complex memory models, threading problems, 739
Composite formatting, 21
Concat()
method, 618
Concatenating strings, 95
Concurrent collection classes, 840–841
Concurrent operations, definition, 736
ConcurrentBag<T>
, 840
ConcurrentDictionary<T>
, 840
ConcurrentQueue<T>
, 840
ConcurrentStack<T>
, 840
Conditional
clauses, catching exceptions, 438
expressions, guidelines, 124
logical operators, overloading, 400
Conditions, 111
ConnectionState
, 372
Consequence statements, 111
Console executables, 404
Console output
comments, types of, 24
composite formatting, 21
format items, 21
format strings, 21
formatting with string interpolation, 20
ConsoleListControl
, 327–331, 336
const
field, encapsulation, 277–278
Constants
declaring, 107
definition, 106
guidelines, 106
vs. variables, guidelines, 106
Constraints on type parameters. See also Contravariance; Covariance.
class type constraints, 477–478
constructor constraints, 480
interface type constraints, 476–477
multiple constraints, 479
non-nullable value types, 478–479
Constraints on type parameters, limitations
array types, 484
combining class type with class
, 482
combining class type with struct
, 482
delegate types, 484
enumerated types, 484
OR criteria, 483
restricting inheritance, 482
sealed types, 484
Construction initializers, 261–262
Constructor constraints, 480
calling one from another, 261–262
centralizing initialization, 262–263
collection initializers, 258–259
definition, 255
exception propagation from, 428
finalizers, 259
in generic types, declaring, 468–469
guidelines, 261
introduction, 254
new
operator, 256
overriding base classes, 313–314
Contains()
method, 651–652, 661–662
ContainsValue()
method, 656
Context switch, definition, 736
Context switching, 737
Contextual keywords, 6, 679–680
Continuation clauses, query expressions, 637–638
Continuation tasks, 757
continue
statement
guidelines, 144
syntax, 109
ContinueWith()
method, 756–758, 760–761, 764–765, 779
Contracts vs. inheritance, 340–341
definition, 495
enabling with in
modifier, 495–497
Control flow. See also Flow control.
guidelines, 139
misconceptions, 784
task continuation, 755
Conversion operators, overloading, 401, 403
Converting
collections to arrays, 646
enums to and from strings, 375–377
between interfaces and implementing types, 338
types. See Types, conversions between.
Cooperative cancellation, definition, 769
Copy()
method, 269–271, 275–277
CopyTo()
method, 646
CoreCLR compiler, 880
Counting
class collection elements with Count()
, 595–596
collection elements, 646
lines within a file, example, 192–193, 194–197
CountLines()
method, 163
definition, 491
enabling with out
modifier, 492–494
guidelines, 498
preventing, 492
type safety, 498
unsafe covariance in arrays, 497–498
Covariant conversion
definition, 491
restrictions, 494
.cpp
file, C++ vs. C#, 174
CPU (central processing unit), definition, 734
Create()
factory method, 723–724
.cs file extension, 3
csc.exe
compiler, 3
CTS (Common Type System), 27, 892, 896
Curly braces ({ }
)
formatting code, 13
omitting, 116
as string literals, 54
CurrentTemperature
property, 546–547
Custom asynchronous methods, 787–791
Custom dynamic objects, 726–728
Custom serialization attributes, 714–715
Customizing
collection interfaces. See Collection interfaces, customizing.
collections. See Collections, customizing.
IEnumerable
interface. See Iterators.
IEnumerable<T>
interface. See Iterators.
IEnumerator<T>
interface. See Iterators.
Customizing, exceptions
guidelines, 448
serializable exceptions, 449
Data
on interfaces, 327
Data types. See Types.
Deadlocks. See also Thread synchronization.
causes of, 832
non-reentrant locks, 832
prerequisites for, 832
threading problems, 740
Deallocating memory, finalizers, 423
Debugging with preprocessor directives, 154
Declaring
constants, 107
delegate types, 510
finalizers, 422
generic classes, 464
generic delegate types, events, 562
methods. See Methods, declaring.
Default
keyword, 73
Default(bool)
keyword, 73
Default(int)
keyword, 73
Deferred execution
standard query operators, 597–598, 600–601
#define
preprocessor directive, 153, 155
Delaying code execution. See Thread synchronization, timers.
Delegate operators, 550–552. See also specific operators.
Delegates
-= (minus sign, equal sign), delegate operator, 550
creating with a statement lambda, 517–518
definition, 506
executing unsafe code, 872–873
guidelines, 525
immutability, 513
invocation sequence diagram, 553
mapping to function pointers, 861–862
method group conversion, 512
method names as arguments, 511–512
multicast, 543, 547. See also Coding the observer pattern with multicast delegates.
nesting, 510
with the null-conditional operator, 128
passing with expression lambdas, 520
synchronous, 751
vs. tasks, 751
thread safety, 550
delete
operator, C++ vs. C#, 224
Delimited comments, 24
DenyChildAttach
enum, 758
Deprecated APIs, 712
Dequeue()
method, 662
Dereferencing pointers, 869–871
abstract classes. See Abstract classes.
casting between base and derived types, 293–294
casting with inheritance chains, 294–295
classes derived from System.Object
, 320–321
data conversion with the as
operator, 322–323
defining custom conversions, 295–296
determining the underlying type, 321–322
explicit cast, 294
extension methods, 299
implicit base type casting, 293–294
implicit conversion, 294
multiple inheritance, simulating, 299–301
overriding base classes. See Base classes, overriding.
private
access modifier, 296–297
protected
access modifier, 297–298
Deserialize()
method, 714
Deserializing
Deterministic finalization with the using
statement, 423–426
Diagramming multiple inheritance, 345
Dictionary classes
customized collection sorting, 649–651
definition, 653
diagrams, 654
hash tables, 656
inserting elements, 654
removing elements, 656
Dictionary collection classes, 644–646
Dictionary collections, 653–657
Dictionary keys, comparing, 658–659
Directives. See Preprocessor directives.
DirectoryCountLines()
method, 193–199
Directory.GetFiles()
method, 625, 632
DirectoryInfoExtension.Copy()
method, 269–271, 275–277
DirectoryInfo.GetFiles()
method, 275, 607
Disassembling CIL, tools for, 33. See also ILDASM
.
Discarding duplicate collection members, 639–640
DispatcherSynchronizationContext
, 794
Disposable tasks, 774
Dispose()
method, 424–425, 426–427
Distinct()
method, 618, 639–641
Division with bit shifting, 130
.dll
file extension, 4
DLL (Dynamic Load Library) file extension, 4
Documents
Dollar sign, at sign ($@
), string interpolation, 50
Dollar sign ($
), string interpolation, 48
Dot (.
) dot operator, 126, 871
DotGNU Portable NET compiler, 880
Double quotes (“ “), coding string literals, 48
double
type, precision, 97
Double.TryParse()
method, 215
Dropping namespaces. See using
directive.
Dump()
method, 337
Duplicate names for attributes, 707
dynamic
as System.Object
, 723–724
dynamic
directive, 721
Dynamic Load Library (DLL) file extension, 4
Dynamic member invocation, 722
dynamic
principles and behaviors, 721–723
dynamic
type. See Programming with dynamic objects.
#elif
preprocessor directive, 153, 154
Eliminating namespaces. See using
directive.
Ellipsis (...) binary/assignment operator, 399
Else
clauses, 111
#else
preprocessor directive, 153, 154
E-mail domain, determining, 149
Empty<T>
method, 667
Encapsulation. See also Access modifiers.
definition, 224
introduction, 219
public constants, permanent values, 278–281
Encryption for documents, 715–716
#endif
preprocessor directive, 154
#endregion
preprocessor directive, 158–159
Enqueue()
method, 662
Enter()
method, 366, 821, 823, 825–826
EntityDictionary
, 479, 500–501
EntityDictionary<TKey, TValue>
, 480
Enumerated types, constraint limitations, 484
casting between arrays of, 375
conversion to and from strings, 375–377
defining, 372
joining values, 378
replacing Boolean values, 372
underlying type, 373
enums, guidelines
creating enums, 374
default type, 373
enum flags, 379
string conversions, 377
Equal sign, greater than (=>
) lambda operator, 517, 524
Equal sign (=
) assignment operator
vs. ==
(equality operator), C++ vs. C#, 118
assigning variables, 16
definition, 16
precedence, 92
Equal signs (==
) equality operator
in place of = (equal sign) assignment operator, 119–120
Equals()
equality operator
requirements for equality comparisons, 659–660
Equals()
method, overloading, 362
Error handling. See also Exception handling.
class collections, 587
Error messages, disabling/restoring, 156–157
#error
preprocessor directive, 153, 155–156
Errors. See also Exception handling.
emitting with directives, 155–156
infinite recursion error, 194
reporting. See Exception handling.
Escape sequences
\
(single backslash character), 46
displaying a smiley face, 47–48
list of, 47
, tab character, 46
Even()
method, 653
Event notification
with multiple threads, 830
thread-safe, 831
Events. See also Coding the observer pattern.
declaring generic delegate types, 562
encapsulating the publication, 560
encapsulating the subscription, 559–560
Exception conditions, 438
Exception handling. See also Errors; specific exceptions.
with AggregateException,
parallel loop iterations, 803–804
catch blocks, 205–206, 211–212
common exception types, 210. See also specific types.
for expected situations, 214–215
guidelines, 212, 215, 436, 442–446
handling order, 207
Java vs. C#, 440
multiple exception types, 433–436
passing null exceptions, 434
program flow, 206
reporting errors. See throw
statement; Throwing exceptions.
task-based asynchronous pattern, 779–780
throwing exceptions, 203–204, 212–215. See also throw
statements.
Exception propagation from constructors, resource cleanup, 428
Exceptions, custom. See also Errors.
guidelines, 448
serializable exceptions, 449
Exchange<T>
method, 829
Exclamation point, equal sign (!=
) inequality operator
testing for inequality, 119–120
Exclamation point (!
) logical NOT operator, 122
Excluding/including code with preprocessor directives, 154
ExecuteSynchronously
enum, 759
Execution time, definition, 27
Exit()
method, 366, 823, 825–826
Exiting a switch section, guidelines, 144
Explicit deterministic resource cleanup, C++ vs. C#, 224
Explicit implementation of interfaces, 334, 336–337
Explicit member implementation, 334–335
Explicitly declared parameter types, 518
Expression bodied methods, 174
Expression lambdas, 520
Expression trees
BinaryExpression
, 535
building LINQ queries, 537–538
Compile()
method, 535
definition, 533
lambda expressions as data, 534–535
LoopExpression
, 535
MethodCallExpression
, 535
NewExpression
, 535
ParameterExpression
, 535
UnaryExpression
, 535
Extensible Markup Language (XML). See XML (Extensible Markup Language).
Extension methods
definition, 276
derivation, 299
inheritance, 299
reflection support for, 723
requirements, 276
extern
alias directive, 413
extern
methods, 851
calling with P/Invoke, 858–860
declaring with P/Invoke, 850–851
Factory methods, generic types, 471–472
FCL (Framework Class Library), 893, 896
Fibonacci calculator, 135
Fibonacci numbers/series, 135
Fields
declaring as volatile
, 828
getter/setter methods, 237–238
virtual, properties as, 249–250
File extensions, 3. See also specific extensions.
FileInfo
collections, projecting, 633–634
FileInfo
object, 593, 625, 633
Filename matching class name, Java vs. C#, 4
Files
FileSettingsProvider
, 341
FileStream
property, 429
Filtering collections
definition, 622
predicates, 631
Finalization
guidelines, 428
Finalization queue, resource cleanup, 426
deallocating memory, 423
declaring, 422
deterministic finalization with the using
statement, 423–426
Finding
even elements in collections, 653
multiple items in collections, 652–653
Fixing (pinning) data, 866–868
FlagsAttribute
, 380–381, 708–709
Flatten()
method, 780
Flattening a sequence of sequences, 638–639
flNewProtect
, 854
binary float, 39
for financial calculations. See decimal
type.
Flow control. See also Control flow.
definition, 734
introduction, 107
Flow control statements. See also specific statements.
alternative, 111
block statements. See Code blocks.
Boolean expressions, evaluating. See if
statement.
break
, 110
code blocks, 114–116. See also Scope.
combining. See Code blocks.
conditions, 111
consequence, 111
continue
, 109
definition, 89
goto
, 110
indentation, 115
scopes, 116–118. See also Code blocks.
true/false evaluation, 111
For
loops
foreach
loops, class collections
with arrays,
code example, 586
with IEnumerable
interface, 587
with IEnumerable<T>
interface, 583–585
modifying collections during, 587–588
ForEach()
method, 806
foreach
statement, 109, 140–142
Formal declaration, methods. See Methods, declaring.
Formal parameter declaration, 171–172
Formal parameter list, 172
Format items, 21
Format()
method, 51
Format strings, 21
FormatMessage()
method, 855
Formatting
numbers as hexadecimal, 43
with string interpolation, 20
strings, 54
Forward slash (/
) division operator
precedence, 92
Forward slash (/) in XML, 25
Framework Class Library (FCL), 893, 896
Frameworks, definition, 26
FromCurrentSynchronizationContext()
method, 793
Full outer join, definition, 604
Function pointers, mapping to delegates, 861–862
Functions
global, C++ vs. C#, 266
pure virtual, 317
Collect()
method, 419
introduction, 418
managed execution, 28
root references, 418
Garbage collector, definition, 223
GC.ReRegisterFinalize()
method, 429
General catch blocks, 211–212, 440–442
General purpose delegates, 524–525
Generic classes
declaring, 464
type parameters, 464
undo, with a generic Stack
class, 461
Generic delegates, events, 564–566
Generic internals
instantiating based on reference types, 500–501
instantiating based on value types, 500–501
introduction, 498
Generic methods
constraints on type parameters, 481–482
guidelines, 491
specifying constraints, 489–490
Generic types
arity (number of type parameters), 471–472
constraints. See Constraints on type parameters.
constructors, declaring, 468–469
finalizers, declaring, 468–469
generic classes, 461
guidelines, 473
implementing, 467
interfaces, description, 466–467
interfaces, implementing multiple versions, 467–468
multiple type parameters, 470–471
overloading a type definition, 471–472
parameterized types, 461
specifying default values, 469–470
type parameter naming guidelines, 465–466
Generic types, generic classes
declaring, 464
type parameters, 464
undo, with a generic Stack
class, 461
Generic types, reflection on
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
Generics, C# without generics
multiple undo operations, 456–459
System.Collection.Stack
class, 456–459
type safety, 459
using value types, 459
get
keyword, 240
GetCurrentProcess()
method, 851
GetCustomAttributes()
method, 701–702
GetDynamicMemberNames()
method, 729
GetEnumerator()
method, 588, 669
GetFiles()
method, 163, 275, 607, 625, 632
GetFirstName()
method, 240
GetGenericArguments()
method, 693–694
GetHashCode()
method, 362, 385–387, 659–660
GetInvocationList()
method, 557–558
GetLastError
API, 855
GetLastName()
method, 240
GetProperties()
method, 685–686
GetResponse()
method, 776
GetResponseAsync()
method, 779
GetReverseEnumerator()
method, 680–681
Getter/setter methods, 237–238, 251–252
GetType()
method, 520, 685–686
GetUserInput()
method, 170, 172, 182
GetValue()
method, 691
GhostDoc tool, 418
Global methods, C++ vs. C#, 171
Global variables and functions, C++ vs. C#, 266
Greater than, equal sign (>=
), greater than or equal operator, 120, 396–397
Greater than sign (>
), greater than operator, 120, 396–397
Greater than signs, equal (>>=
) shift right assignment operator, 130
Greater than signs (>>
), shift right operator, 130, 397–399
GreaterThan()
method, 512
group
clause, 623
Grouping query results, 634–637
Guest computer, definition, 872
.h
file, C++ vs. C#, 174
Hash tables
dictionary classes, 656
Header files, C++ vs. C#, 174
HelloWorld
program
Hexadecimal numbers
formatting numbers as, 43
specifying as values, 43
HideScheduler
enum, 759
Hot tasks, 752
Hungarian notation, 7
Hyper-Threading, definition, 734
Hyphens (-
), in identifier names, 7
IAngle.MoveTo
interface, 367
IAsyncAction<T>
interface, 876
ICloseable
, 876
ICollection<T>
interface, 646
IComparable
interface, 649–650
IComparable<string>.CompareTo()
method, 649–650
Id
property, 755
Identifiers
camelCase, 7
definition, 6
guidelines for, 7
keywords as, 8
IDictionary<TKey
, TValue>
, 644–646, 653–657
IDisposable
interface
cleaning up after iterating, 586
resource cleanup, 424–425, 426–427
Task
support for, 774
in WinRT. See ICloseable
.
IDisposable.Dispose()
method, 424–425, 426–427
IDistributedSettingsProvider
interface, 346
class diagram, 584
CopyTo()
method, 646
Count()
method, 646
customizing. See Iterators.
foreach
loops, class collections, 587
.NET class collections, 582
class diagram, 584
customizing. See Iterators.
.NET class collections, 582
in WinRT. See IIterable<T>
.
IEnumerator<T>
interface, customizing. See Iterators.
IEqualityComparer<T>
interface, 658–659
#if
preprocessor directive, 153, 154
if
statements, 108, 111, 114, 150
if
/else
statements, examples, 111, 113
IFileCompression
interface, 326–327
IFormattable
interface, 362, 370, 479, 483–485
IIterable<T>
, 876
IL. See CIL (Common Intermediate Language).
IL Disassembler. See ILDASM
.
IListable
interface, 327–335, 342–343
ILMerge.exe
utility, 890
Immutability
delegates, 513
value types, 357
Immutable strings, syntax, 17
Implicit conversion, 65, 68–69
Implicit deterministic resource cleanup, C++ vs. C#, 224
Implicit implementation interfaces, 334, 336–337
Implicit local variables, anonymous types, 572–576
Implicit member implementation, interfaces, 335–336
Implicit nondeterministic resource cleanup, C++ vs. C#, 224
Implicit overriding, Java vs. C#, 304
Implicitly typed local variables, 60–61
Importing types from namespaces. See using
directive.
Imports
directive, 176
In
type parameter, 526
Increment/decrement operators (++
, --
)
C++ vs. C#, 105
postfix increment operator, 104–105
post-increment operator, 103
prefix increment operator, 104–105
pre-increment operator, 103–104
Indentation, flow control statement, 115
Indexers
Infinite recursion error, 194
Information hiding. See Encapsulation.
base classes, 302
base type, 220
child type, 220
constraint limitations, 482
constraints on type parameters, 480–482
extension methods, 299
“is a kind of” relationships, 290
multiple, C++ vs. C#, 299
parent type, 220
purpose of, 290
specializing types, 221
subtypes, 220
super types, 220
Initializing attributes with a constructor, 701–705
Inner
classes, Java, 283
InnerExceptions
property, 558, 764, 780, 804
Insert
method, 58
Inserting
elements in dictionary classes, 654
new elements in collections, 651–652
Instance fields. See also Static, fields.
definition, 225
Instance methods
adding to a class, 276
definition, 51
Instantiating
interfaces, 334
Integer literals, determining type of, 41–42
Integral types, 96
Interface type constraints, 476–477
vs. abstract classes, 325–326, 338
vs. attributes, 349
contracts vs. inheritance, 340–341
converting between interfaces and implementing types, 338
data, 327
defining, 327
deriving one from another, 338–341, 346–347
instantiating, 334
method declarations in, 327
naming conventions, 327
post-release changes, 346
Interfaces, generic types
finalizers, declaring, 468–469
implementing multiple versions, 467–468
Interfaces, implementing and using
accessing methods by name, 336
code example, 328–332, 332–333
explicit implementation, 334, 336–337
explicit member implementation, 334–335
implicit implementation, 334, 336–337
implicit member implementation, 335–336
overview, 327
Interfaces, multiple inheritance
diagramming, 345
working around single inheritance, 343–344
internal
access modifiers on type declarations, 407–408
internal
accessibility modifier, 409
Intersect()
method, 618
into
keyword, 637
InvalidAddressException
, 446
InvalidCastException
, 366
I/O-bound latency, definition, 732
IObsolete
interface, 349
IOrderedEnumerable<T>
interface, 602
IProducerConsumerCollection<T>
, 840–841
IQueryable<T>
interface, 619
IReadableSettingsProvider
interface, 338–341
IReadOnlyPair<T>
interface, 492–494
“Is a kind of” relationships, 290
“Is a” relationships, 293–294, 317
Is
operator, verifying underlying types, 321–322
IsAlive
property, 744
IsBackground
property, 743
IsCancellationRequested
property,
monitoring, 770–771
IsCompleted
property, 754
ISerializable
interface, 449, 715–716
ISettingsProvider
interface, 339–341, 346–347
IsInvalid
, 858
IsKeyword()
method, 628
Items
property, 468
Iterating over
class collections using IEnumerable<T>
. See IEnumerable<T>
interface.
class collections using While()
, 584
properties of objects in a collection. See Reflection.
creating a language construct from, 668
defining, 668
functional description, 678–679
guidelines, 676
multiple in one class, 680–681
nested, 676
recursive, 676
returning values from, 669–671, 673–674
struct
vs. class
, 677
yield break
statement, 677–678
yield return
statement, 670–671, 673–676, 681
ITrace
interface, 337
IWriteableSettingsProvider
interface, 341
Java, similarities to C#, 2
Java vs. C#
classes spanning multiple files, 4
exception specifiers, 440
filename matching class name, 4
implicit overriding, 304
importing namespaces with wildcards, 176
inner classes, 283
main()
method, 10
nested classes, 283
partial class, 4
virtual methods by default, 303
JavaScript vs. C#
var
keyword, 575
Variant, 575
void*
, 575
JIT (just-in-tme) compiler, 881
Jitting, 881. See also Compilers; Compiling; Just-in-time compilation.
Join()
method, 607–610, 743–744
Join operations. See Standard query operators, join operations.
Jump statements
if
statement, 150
Just-in-time compilation, 26
KeyNotFoundException
, 656
Keywords. See also specific keywords.
contextual, 6
definition, 4
as identifiers, 8
incompatibilities, 6
list of, 5
placement, 4
Kill()
method, 789
Lambda calculus, 524
=> (equal sign, greater than) lambda operator, 517, 524
capturing loop variables, 531–533
closed over variables, 528–530
closures, 531
definition, 516
explicitly declared parameter types, 518
expression lambdas, 520
GetType()
method, 520
guidelines, 518
lifetime of captured variables, 530
outer variable CIL implementation, 530–531
predicate, definition, 520, 591
returning a bool
, 520
sequence of operations, 599
typeof()
operator, 520
Lambdas, asynchronous, 786–787
Language contrast. See specific languages.
Language interoperability, 27
Latency
asynchronous high-latency operations with the TPL, 777–781
definition, 732
synchronous high-latency operations, 775–777
LazyCancellation
enum, 759
Left outer join, definition, 604
Left-associative operators, 93
Length
Less than, equal sign (<=
) less than or equal operator, 120, 396–397
Less than sign (<
) less than operator, 120, 396–397
Less than signs, equal (<<=
) shift left assignment operator, 130
Less than signs (<<
) shift left operator, 130, 397–399
Libraries
BCL (Base Class Library), 893
FCL (Framework Class Library), 893
file extension, 4
PCLs (portable class libraries), 406
TPL (Task Parallel Library), 733
Library implementation. See CLS (Common Language Specification).
Lifetime of captured variables in lambda expressions, 530
#line
preprocessor directive, 153, 157–158
Line-based, statements, Visual Basic vs. C#, 11
Linked list collections, 663
LinkedListNode<T>
, 663
LinkedList<T>
, 663
LINQ queries
building with expression trees, 537–538
with query expressions. See Query expressions with LINQ.
Liskov, Barbara, 667
Lists. See also Collections.
List<T>
covariance, 492
List<T>.Sort()
method, 649–650
case sensitivity, suffixes, 42
definition, 39
exponential notation, 42
strings, 48
Loading
Local variable scope, C++ vs. C#, 118
assigning values to, 14, 16–17
casing, 15
guidelines for naming, 15
unsynchronized, 820
lock
objects, 825
Lock performance, 825
Locking
guidelines, 827
on this
, typeof
, and string
, 826–827
threading problems, 740
Logical operators, 131–133. See also Boolean expressions, logical operators.
long
type, 36
LongRunning
enum, 758
LoopExpression
, 535
Loops
break
statement, 110
continue
statements, 109
with decrement operator, 102–105
goto
, 110
guidelines, 139
iterations, definition, 136
switch
, 110
Loops, jump statements
if
, 150
lpflOldProtect
, 853