Please note that index links point to page beginnings from the print edition. Locations are approximate in e-readers, and you may need to page down one or more times after clicking a link to get to the indexed material.
References to figures are in italics.
-- operator, 49
$ operator, 107
% operator, 47
%= operator, 47
* operator, 47
*= operator, 47
@, 106
\\, 105
^ operator, 107
| operator, 108
implementing a + operator overload (example code), 440–442
++ operator, 48
+= operator, 48
< operator, 76–77
<= operator, 76–77
-= operator, 47
/= operator, 47
!= operator, 76
== operator, 76
=> operator, 215
> operator, 76–77
>= operator, 76–77
\, in escape sequences, 105–106
- operator, 47
/ operator, 47
A
abstract inheritance, 172
comparison of virtual and abstract declarations, 174–175
declaring abstract classes, 172–173
declaring abstract methods, 172
declaring abstract properties, 173
example code, 173–174
overriding abstract methods, 172
overriding abstract properties, 173
See also inheritance
abstract keyword, 172
Add( ) method, 120, 192, 203, 328–329
addition, 46
AND operator, 77
example code, 81
anonymous functions, 214
with delegates, 214–216
with Func<T,TResult> delegates, 216–218
lambda syntax, 215
ArrayList collections, 195–196
ArrayList introduction (example code), 197–198
determining object types, 196
methods, 196–197
arrays, 32
creating JSON arrays, 407–408
declaring array type and name first and size allocation later, 33
declaring array type, name and size together, 33
declaring single-dimensional arrays (example code), 34–35
defined, 26
elements, 32
enumerations with (example code), 42–43
indexes, 32
multidimensional arrays, 37–38
sorting, 36–37
syntax, 32–34
Attribute class, inheriting from, 423
attributes, 420–421
creating a validation attribute (example code), 432–433
custom attributes, 423–426
implementing a custom attribute, 424
inheriting from the Attribute class, 423
inspecting attributes (example code), 425–426
optional AttributeUsage attributes, 423–424
querying, 424–425
querying attribute targets, 430–433
reflection, 427–429
tracking bug fixes (example code), 430–431
B
backslashes, in escape sequences, 105–106
binary data
randomly accessing, 374–375
writing and reading, 367–371
See also buffered binary data
BinaryReader, 368–369
BinaryWriter, 368
block scope, 68
break statements, 91–93
breakpoints, 15–16
bridge tables, 325–327
deleting bridge table objects, 330–331
inserting bridge table objects, 328–330
querying bridge table objects, 327–328
browsing database content, 248–249
buffered binary data, reading, 371–374
C
C#, history of, 4
Call Stack window, 17
cardinality, 310–311
case sensitivity, 104
casting, 32
classes, 135
compared to structs, 151
constructors, 137–140
declarations, 136–137
default constructors, 138
defined, 5
destructors, 143–145
get accessors, 141–142
instances, 136
for managing database data, 335–338
object creation (example code), 138–140
objects, 136
properties, 141–143
scope, 68
sealing, 180–181
set accessors, 141–142
this keyword, 137
See also individual classes; partial classes; static classes
Clear( ) method, 192, 197, 201, 203
Close( ) method, 350
code libraries, 152
CommandType property, 344, 352
comments, 5–6
CompareTo( ) method, 121, 129–130
comparison operators, 76–77
See also operators
compiling programs, 7
composite primary keys, 309
equal joins, 317–319
compound operator, 48
concatenation, 104
conditional expressions, 74
See also control structures
conditional operators, 82–83
conditional structures, 78
if-else statements, 78–81
connection string parameters, 242
connection strings, referencing in C#, 339–340
ConnectionStrings property, 339–340
Console class, 5
constants, 30
defined, 26
names, 30
syntax, 30
constructors, 137–140
inherited constructor overloading, 167
ContainsKey( ) method, 201, 203
ContainsValue( ) method, 203
continue statements, 93–94
control structures, 74
indentation and alignment, 74–75
nesting, 75
custom attributes, 423–426
See also attributes
D
data types, 26–27
and variables (example code), 29–30
database setup
accessing database resources for this book, 242
browsing database content with Visual Studio, 248–249
creating a database connection string, 242–248
examining database column detail, 250–251
database types, 243
databases
cardinality, 310–311
composite primary keys, 309
foreign keys, 309–310
primary keys, 309
DataColumn class, 335–336
DataRow class, 336
date and time handling
DateTime type, 122–130
TimeSpan type, 118–122
DateTime type
addition, 126–128
comparison, 129–130
format specifiers, 124–125
properties, 122–124
subtraction, 128–129
syntax, 122
system time, 122
DbContext class, 260
DbSet class, 260
debugging, 13
breakpoints, 15–16
Call Stack window, 17
errors and warnings, 14–15
Locals window, 17
logging data, 21–23
resuming program execution, 16
stepping into methods, 16
stepping over methods, 16
tooltips, 16
try-catch blocks, 19–21
Watch window, 19
decrement operator, 49
deferred execution, 266
delegates, 212
anonymous functions with, 214–216
anonymous functions with Func<T,TResult> delegates, 216–218
simple delegates (example code), 213
syntax, 212–213
delimiters, 101–102
deserialization, 414–415
mapping to custom property names during deserialization, 415
XML, 397–399
See also serialization
destructors, 143–145
Dictionary collections, 200
Dictionary introduction (example code), 202–203
iterating through dictionary items, 201
methods, 201
displaying local currency, 99
DisplayLocation( ) method, 135
Distinct( ) extension method, 299
Distinct( ) method, 280–281
division, 47
dynamic parsing, 410–411
E
editing code, 7
editing strings, 103–105
EDM. See Entity Data Model (EDM)
EF. See Entity Framework
elements, 32
else if statements, 78–79
else statements, 79
encapsulation
accessibility, 134–135
defined, 134
members, 134
private access, 135
protected access, 135
public access, 135
entities, 260
Entity Data Model (EDM), 254
adding an Entity Data Model to your code project, 254–259
Copy to Output Directory property, 258–259
navigation properties, 312
querying database objects, 260–262
querying entities, 261–262
querying stored procedures, 262–263
replacing, 264–265
updating, 263–264
Entity Framework, 254
and SQL, 334
enum keyword, 40
enumerations, 39
with arrays (example code), 42–43
defined, 26
determining the enumerator count, 41
example code, 41–42
methods, 40–41
naming, 40
syntax, 40
equal joins, 313
with composite keys, 317–319
with method-based syntax, 314–315
with navigation properties, 315–316
with query syntax, 313–314
equals operators, 76
errors, 14–15
escape sequences, 105–106
escaped backslash, 105
event declaration syntax, 218
example code, 220–221
raising an event, 219
subscribing to an event, 219
unsubscribing from an event, 219
Exception class, 19–21
properties, 20
ExecuteReader( ) method, 350, 354
executing programs, 7
exponentiation, 47
Extensible Markup Language. See XML
extension methods, 294
lambda syntax, 295
F
FileMode, 368
filtering, 278
applying a where filter (example code), 278–279, 296–297
basic filtering, 278
with a collection of values, 279, 297–298
retrieving similar matches, 280
selecting distinct rows, 280–281
with string comparison methods (example code), 280, 298–299
Find and Replace dialog, 11–12
FoodStore database
accessing database resources for this book, 242
creating a connection string for database development beginners (example), 243–245
entity data model conceptual view, 444, 445
entity relationship diagram, 444
generating a connection string to SQL Server, 245–248
FoodStore_log.ldf file, 243
FoodStore.mdf file, 243
foreach loops, 90–91
foreign keys, 309–310
from keyword, 275
Func<T,TResult> delegates, 216–218
G
generic interfaces, 231–233
generic types, 186
declaring and initializing objects of a generic class or struct, 187–188
declaring generic templates, 186–187
generic class introduction (example code), 188–189
generic constructors, 187
generic methods, 188
generic properties, 188
generic variables, 188
multiple generic type parameters (example code), 190–191
get accessors, 141–142
GetBytes( ) method, 53
GetCustomAttributes( ) method, 425
GetLength( ) method, 39
GetNames( ) method, 41
greater than operator, 76–77
greater than or equal operator, 76–77
grouping
on multiple columns, 303
gutter, 16
H
Hashtable collections, 203
Hashtable introduction (example code), 204–205
iterating through Hashtable items, 204
methods, 203
history of C#, 4
IComparable interface, 234–236
IEnumerable interface, 266
if statements, 78
if-else statements, 78–81
immediate loading, 266–267
vs. lazy loading, 278
immutable strings, 64
in keyword, 275
increment operator, 48
indentation, 6–7
control structures, 74–75
methods, 60
indexes, 32
IndexOf( ) method, 100–101
inheritance
declaring a child class, 163
defined, 162
example code, 164–165
inherited constructor overloading, 167
inheriting from the Attribute class, 423
introduction to, 162–163
passing parameters to parent constructors (example code), 167–169
protected members, 163
See also abstract inheritance; virtual inheritance
Insert( ) method, 105, 192, 196
InsertRange( ) method, 192, 196
interfaces
auto-generating a code shell of required members, 226
example code, 225
existing .NET interfaces, 233–236
explicit interface implementation, 227–229
generic, 231–233
hierarchies, 230–231
implicit interface implementation, 229
syntax, 224–225
IQueryable interface, 266, 278
IReadOnlyCollection interfaces, 233
J
JArray class, 407–408
JavaScript Object Notation. See JSON
JObject class, 406
Join( ) method, 102
joins, 308
equal joins, 313–319
joining more than two collections in one query, 324–325
outer joins, 319–324
JSON
creating a complex JSON object (example code), 408–409
creating JSON arrays, 407–408
customizing property names during, 414
deserialization, 414–415
dynamic parsing, 410–411
dynamic types, 406–407
introduction to, 404–405
JArray class, 407–408
JObject class, 406
mapping to custom property names during deserialization, 415
serialization, 411–413
Json.NET, 405–406
K
KeyValuePair types, 199
KeyValuePair introduction (example code), 199–200
keywords. See individual keywords
L
lambda operator, 215
Language-Integrated Query. See LINQ
LastIndexOf( ) method, 101
lazy loading, 265–266
vs. immediate data loading, 278
left outer joins, 319–320
with LINQ, 320–321
less than operator, 76–77
less than or equal operator, 76–77
aggregate summary queries, 286–287
assigning custom property names, 276
deserialization, 397–399
filtering, 278–281
full outer joins, 322–324
lazy loading vs. immediate data loading, 278
left outer joins, 320–321
merging similar queries with a union, 285–286
reading one object only, 281
reading queries, 275
referencing properties of an object, 275–276
right outer joins, 321–322
selecting all columns of an object (example code), 277
selecting specific columns of an object (example code), 276–277
adding objects, 288
aggregate summary queries, 301–302
bridging many-to-many relationships, 325–331
deferred execution, 266
defining query columns with the Select( ) method, 295–296
deleting bridge table objects, 330–331
deleting objects, 289–290
equal joins, 313–319
filtering, 296–297
filtering with a collection of values, 297–298
immediate (eager) data loading, 266–267
inserting bridge table objects, 328–330
joining more than two collections in one query, 324–325
joining queries, 308
lazy loading, 265–266
merging similar queries with a union, 301
navigation properties, 312
outer joins, 319–324
querying bridge table objects, 327–328
reading queries, 295
retrieving similar matches, 298–299
selecting distinct rows, 299
subselection, 283–284, 300–301
updating objects, 289
LINQ to XML, 382
additional element selection methods and properties, 387–388
querying elements, 383
querying with method-based syntax and query syntax (example code), 386–387
selecting elements and attributes (example code), 384–385
XAttribute class, 383
XElement class, 382–383
LINQPad, 267
using to observe query execution, 267–270
List collections, 191
List introduction (example code), 193
lists with polymorphism (example code), 194–195
methods, 192
Locals window, 17
logging data, 21–23
loops, 85
break statements, 91–93
comparing loop types, 91
continue statements, 93–94
do while loops, 89–90
foreach loops, 90–91
for loops, 85–88
while loops, 88–89
Managed C++, 4
many-to-many relationships, bridging, 325–331
Match Case, 12
Match Whole Word, 12
Math class, 47
members, 134
sealing, 180–181
metadata, 420
See also attributes
methods
ArrayList methods, 196–197
calling a method, 59–60
creating and implementing methods (example code), 60–61
declaring abstract methods, 172
declaring virtual methods, 169
defined, 5
Dictionary methods, 201
generic methods, 188
Hashtable collections, 203
headers, 58
indentation, 60
List methods, 192
names, 59
overloading, 62–63
overriding abstract methods, 172
overriding virtual methods, 170
parameters, 59
passing arguments by value and by reference, 63–65
passing arguments explicitly by reference, 65–66
passing uninitialized variables by reference, 66–67
return types, 59
return values, 59
scope, 68
static modifiers, 59
syntax, 58–62
void types, 59
See also individual methods
minus assignment operator combination, 47
modulus operator, 47
multidimensional arrays, 37–38
example code, 38–39
length, 39
syntax, 38
multiple-line comments, 5
multiplication, 47
multiplication assignment operator, 47
N
namespace collisions, 153
namespaces, 152–153
defined, 5
example, 153–156
navigation properties, 312
equal joins, 315–316
equal joins on composite keys, 318–319
nested for loops, 86–88
.NET Framework, 4
attributes, 420–421
classes for managing database data, 335–338
existing .NET interfaces, 233–236
Next( ) method, 50
NextDouble( ) method, 51
not equals operators, 76
NuGet Package Manager, 405, 406
numeric format specifiers, 98
displaying local currency, 99
raw text, 98
rounding, 99
rounding with a thousands separator, 99
numeric literals, 27–28
O
object-relational mapping frameworks, 254
objects, 136
adding using LINQ to Entities, 288
deleting using LINQ to Entities, 289–290
pluralizing object names, 257
updating using LINQ to Entities, 289
operator overloading, 436
implementing a + operator overload (example code), 440–441
method requirements, 437
operator overloads for Budget objects (example code), 437–439
operators that can be overloaded, 437
operators, 46–49
comparison operators, 76–77
conditional operators, 82–83
OrderBy( ) extension method, 299–300
orderby keyword, 282–283
OrderByDescending( ) extension method, 299–300
ORM frameworks, 254
out keyword, 66–67
outer joins, 319
full outer joins, 322–324
left outer joins, 319–321
right outer joins, 321–322
overloading methods, 62–63
override keyword, 170, 172, 173
P
parsing strings, 99–103
partial classes, 157
example, 157–159
partial keyword, 157
plain text, writing and reading, 362–366
polymorphism, 176
example code, 177–178
lists with polymorphism (example code), 194–195
practical polymorphism (example code), 178–179
post-decrement expressions, 49
post-increment expressions, 48
Pow( ) method, 47
precedence, 69
example code, 69–70
pre-decrement expressions, 49
pre-increment expressions, 48
primary keys, 309
R
random binary access, 374–375
Random class, 50–52
randomization, 50
default random constructor, 50–52
default random generation (example code), 51
generating a random number with a random seed (example code), 54
generating a random seed, 52–53
generating random double numbers, 51
generating random integer numbers, 50
improper random number generation (example code), 52
seeded random constructor, 52–54
raw text, 98
Read( ) method, 350
ref keyword, 66
refactoring, 13
reflection, 427–429
regular expressions, 106–107
complex regular expressions (example code), 112–113
ends with, 107
example code, 108–109
or, 107
for simple patterns (example code), 109–110
starts with, 107
summary of operators, 109, 110–111
relational database theory, 309–311
remainder, 47
Remove( ) method, 105, 192, 197, 201, 203, 330–331
RemoveRange( ) method, 192, 197
Rename dialog, 13
renaming a code file, 11
renaming code structures, 11–13
Replace( ) method, 104
resuming program execution, 16
RNGCryptoServiceProvider class, 53
rounding, 99
with a thousands separator, 99
S
SaveChanges( ) method, 330
scope
avoiding scope conflicts, 70
block scope, 68
class scope, 68
defined, 68
example code, 69–70
method scope, 68
sealed modifiers, 179–180
sealed classes and members, 180–181
Seek( ) method, 374–375
Select( ) extension method, 295–296
select keyword, 275
serialization, 411–413
customizing property names during, 414
customizing XML serialization, 396–397
deserialization, 414–415
XML, 394–397
set accessors, 141–142
shared structures, 145
single-line comments, 5
Solution Explorer, 7
exiting, 11
opening, 11
saving, 11
Sort( ) method, 36–37
arrays, 36–37
with multiple columns, 282–283
Split( ) method, 101–102
SQL
executing SQL with the SqlDataReader (example code), 350–352
executing with the SqlDataAdapter class (example code), 342–343
managing the database connection, 349–357
memory-based data management with the SqlDataAdapter class, 341–343
overview, 334–335
stored procedures, 335
SqlDataAdapter class, 341
initializing a SqlDataAdapter object, 341
managing stored procedures with, 344–349
retrieving data with, 341
timing out, 341
SqlDataReader class
managing stored procedures with, 352–357
retrieving data with, 350
streaming data with, 349–357
Sqrt( ) method, 47
square root, 47
StartsWith( ) method, 102–103
static classes, 147
static data members, 145–146
static methods, 146
static structures, 145
declaring, 145–150
practical application of static modifiers (example code), 149–150
static vs. non-static structures (example code), 147–148
stepping into methods, 16
stepping over methods, 16
stored procedures, 335
managing with SqlDataAdapter class, 344–349
managing with the SqlDataReader class, 352–357
StreamReader class, reading plain text, 364–366
StreamWriter class, 21–22
writing plain text, 362–364
strings
adjusting letter case, 104
concatenation, 104
Contains( ) method, 103
converting to other formats, 113–115
editing, 103–105
IndexOf( ) method, 100–101
Insert( ) method, 105
Join( ) method, 102
LastIndexOf( ) method, 101
length, 99–100
parsing, 99–103
Remove( ) method, 105
Replace( ) method, 104
Split( ) method, 101–102
StartsWith( ) method, 102–103
Substring( ) method, 101
ToLower( ) method, 104
ToUpper( ) method, 104
Trim( ) method, 103
TryParse( ) method, 113, 114–115
verbatim string literals, 106
structs, 150–152
compared to classes, 151
example code, 151–152
subselection, 283–284, 300–301
Substring( ) method, 101
Subtract( ) method, 121, 128–129
subtraction, 48
grouping on multiple columns, 303
switches, 83–84
syntax, 6
anonymous functions, 215
arrays, 32–34
constants, 30
DateTime type, 122
enumerations, 40
event declaration syntax, 218
interface, 224–225
methods, 58–62
multidimensional arrays, 38
TimeSpan type, 118
variables, 28–29
XML, 381
System.Configuration library, adding a reference to, 339–340
T
terminology, 5
ternary operators. See conditional operators
ThenBy( ) extension method, 299–300
ThenByDescending( ) extension method, 299–300
this keyword, 137
TimeSpan type
addition, 120
comparison, 121
properties, 119–120
subtraction, 121
syntax, 118
timing out, 341
ToInt32( ) method, 53
ToLower( ) method, 104
tooltips, 16
ToUpper( ) method, 104
Trim( ) method, 103
try-catch blocks, 19–21
TryGetValue( ) method, 201
TryParse( ) method, 113, 114–115
Type methods, 427
typeof expression, 41
U
Union( ) extension method, 301
Union( ) method, 285–286
value keyword, 141
var keyword, 278
variables
data types and (example code), 29–30
explicit variable conversion, 32
generic variables, 188
implicit variable conversion, 32
names, 29
precedence, 69
reference type, 28
syntax, 28–29
types, 28
value type, 28
verbatim string literals, 106
virtual inheritance, 169
comparison of virtual and abstract declarations, 174–175
declaring virtual methods, 169
declaring virtual properties, 170
example code, 170–172
overriding virtual methods, 170
overriding virtual properties, 170
See also inheritance
Visual Basic .NET (VB.NET), 4
Visual Studio IDE, 8
browsing database content with, 248–249
Call Stack window, 17
compiling and running a program, 10
creating a console application project, 10
debugging, 13–23
exiting the solution, 11
Find and Replace dialog, 11–12
Locals window, 17
opening the solution, 11
Rename dialog, 13
renaming a code file, 11
renaming code structures, 11–13
saving the solution, 11
stopping the application, 10
Watch window, 19
W
warnings, 14–15
Watch window, 19
where keyword, 278–279
writing code, 7
X
XAttribute class, 383
XElement class, 382–383
XML
adding an XML document to a Visual Studio project, 380
adding elements to existing documents, 390–391
creating an XML file (example code), 378–379
creating elements and attributes, 389–390
customizing serialization, 396–397
deleting elements, 393
deserialization, 397–399
element hierarchy, 379–380
introduction to, 378
serialization, 394–397
syntax, 381
updating elements and attributes, 391–393
See also LINQ to XML
XML tag comments, 6