forked from markjprice/cs11dotnet7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter02.dib
473 lines (313 loc) · 11 KB
/
Chapter02.dib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!csharp
#!markdown
# Chapter 2 - Speaking C#
#!csharp
using static System.Console;
#!markdown
## Showing the compiler version
#!csharp
// #error version
#!markdown
## Help for writing correct code
In a .NET Interactive notebook, you do not need to end the last statement with a semi-colon.
#!csharp
// with two compile errors
Console.Writeline("Hello World!")
// fixed
Console.WriteLine("Hello World!")
#!markdown
## Revealing the extent of the C# vocabulary
When running this code inside a .NET Interactive notebook, it lists all possible assemblies, including some used by the extension like `Microsoft.DotNet.Interactive`.
#!csharp
using System.Linq;
using System.Reflection;
// loop through the assemblies that this app references
foreach (var r in Assembly.GetEntryAssembly()
.GetReferencedAssemblies())
{
// load the assembly so we can read its details
var a = Assembly.Load(new AssemblyName(r.FullName));
// declare a variable to count the number of methods
int methodCount = 0;
// loop through all the types in the assembly
foreach (var t in a.DefinedTypes)
{
// add up the counts of methods
methodCount += t.GetMethods().Count();
}
// output the count of types and their methods
Console.WriteLine(
"{0:N0} types with {1:N0} methods in {2} assembly.",
arg0: a.DefinedTypes.Count(),
arg1: methodCount,
arg2: r.Name);
}
#!markdown
## Naming things and assigning values
#!csharp
// let the heightInMetres variable become equal to the value 1.88
double heightInMetres = 1.88;
Console.WriteLine($"The variable {nameof(heightInMetres)} has the value {heightInMetres}.");
#!markdown
## Storing text
#!csharp
char letter = 'A'; // assigning literal characters
char digit = '1';
char symbol = '$';
char userChoice = GetSomeKeystroke(); // assigning from a fictitious function
string firstName = "Bob"; // assigning literal strings
string lastName = "Smith";
string phoneNumber = "(215) 555-4256";
// assigning a string returned from a fictitious function
string address = GetAddressFromDatabase(id: 563);
#!markdown
## Understanding verbatim strings
In a .NET Interactive notebook, a brighter red color is used for escaped characters inside a string value to make them easier to see.
`\t` means tab. `\b` means backspace. `\s` does is not a valid escape character.
#!csharp
string fullNameWithTabSeparator = "Bob\tSmith";
string filePath = "C:\televisions\sony\bravia.txt";
#!csharp
string filePath = @"C:\televisions\sony\bravia.txt";
#!markdown
## Storing numbers
#!csharp
// unsigned integer means positive whole number or 0
uint naturalNumber = 23;
// integer means negative or positive whole number or 0
int integerNumber = -23;
// float means single-precision floating point
// F suffix makes it a float literal
float realNumber = 2.3F;
// double means double-precision floating point
double anotherRealNumber = 2.3; // double literal
#!markdown
## Storing whole numbers
#!csharp
// three variables that store the number 2 million
int decimalNotation = 2_000_000;
int binaryNotation = 0b_0001_1110_1000_0100_1000_0000;
int hexadecimalNotation = 0x_001E_8480;
// check the three variables have the same value
// both statements output true
Console.WriteLine($"{decimalNotation == binaryNotation}");
Console.WriteLine(
$"{decimalNotation == hexadecimalNotation}");
#!markdown
## Writing code to explore number sizes
#!csharp
Console.WriteLine($"int uses {sizeof(int)} bytes and can store numbers in the range {int.MinValue:N0} to {int.MaxValue:N0}.");
Console.WriteLine($"double uses {sizeof(double)} bytes and can store numbers in the range {double.MinValue:N0} to {double.MaxValue:N0}.");
Console.WriteLine($"decimal uses {sizeof(decimal)} bytes and can store numbers in the range {decimal.MinValue:N0} to {decimal.MaxValue:N0}.");
#!markdown
## Comparing double and decimal
#!csharp
Console.WriteLine("Using doubles:");
double a = 0.1;
double b = 0.2;
if (a + b == 0.3)
{
Console.WriteLine($"{a} + {b} equals 0.3");
}
else
{
Console.WriteLine($"{a} + {b} does NOT equal 0.3");
}
#!csharp
Console.WriteLine("Using decimals:");
decimal c = 0.1M; // M suffix means a decimal literal value
decimal d = 0.2M;
if (c + d == 0.3M)
{
Console.WriteLine($"{c} + {d} equals 0.3");
}
else
{
Console.WriteLine($"{c} + {d} does NOT equal 0.3");
}
#!markdown
## Storing Booleans
#!csharp
bool happy = true;
bool sad = false;
#!markdown
## Storing any type of object
#!csharp
object height = 1.88; // storing a double in an object
object name = "Amir"; // storing a string in an object
Console.WriteLine($"{name} is {height} metres tall.");
//int length1 = name.Length; // gives compile error!
int length2 = ((string)name).Length; // tell compiler it is a string
Console.WriteLine($"{name} has {length2} characters.");
#!markdown
## Storing dynamic types
#!csharp
// storing a string in a dynamic object
// string has a Length property
dynamic anotherName = "Ahmed";
// int does not have a Length property
anotherName = 12;
// an array of any type has a Length property
anotherName = new[] { 3, 5, 7 };
// this compiles but would throw an exception at run-time
// if you later store a data type that does not have a
// property named Length
Console.WriteLine($"Length is {anotherName.Length}");
#!markdown
## Specifying and inferring the type of a local variable
*Note*: click **Execute Code** in the following cell to import the namespaces for the subsequent code cell.
#!csharp
using System.IO;
using System.Xml;
#!csharp
var population = 66_000_000; // 66 million in UK
var weight = 1.88; // in kilograms
var price = 4.99M; // in pounds sterling
var fruit = "Apples"; // strings use double-quotes
var letter = 'Z'; // chars use single-quotes
var happy = true; // Booleans have value of true or false
// good use of var because it avoids the repeated type
// as shown in the more verbose second statement
var xml1 = new XmlDocument();
XmlDocument xml2 = new XmlDocument();
// bad use of var because we cannot tell the type, so we
// should use a specific type declaration as shown in
// the second statement
var file1 = File.CreateText(@"C:\something.txt");
StreamWriter file2 = File.CreateText(@"C:\something.txt");
XmlDocument xml3 = new(); // target-typed new in C# 9 or later
#!csharp
class Person
{
public DateTime BirthDate;
}
Person kim = new();
kim.BirthDate = new(1967, 12, 26); // instead of: new DateTime(1967, 12, 26)
#!markdown
## Getting default values for types
The default value of a `string` is `null` which outputs as nothing.
#!csharp
Console.WriteLine($"default(int) = {default(int)}");
Console.WriteLine($"default(bool) = {default(bool)}");
Console.WriteLine(
$"default(DateTime) = {default(DateTime)}");
Console.WriteLine(
$"default(string) = {default(string)}");
#!csharp
int number = 13;
Console.WriteLine($"number has been set to: {number}");
number = default;
Console.WriteLine($"number has been reset to its default: {number}");
#!markdown
## Storing multiple values an array
#!csharp
string[] names; // can reference any array of strings
// allocating memory for four strings in an array
names = new string[4];
// storing items at index positions
names[0] = "Kate";
names[1] = "Jack";
names[2] = "Rebecca";
names[3] = "Tom";
// looping through the names
for (int i = 0; i < names.Length; i++)
{
// output the item at index position i
Console.WriteLine(names[i]);
}
#!markdown
## Making a value type nullable
#!csharp
int thisCannotBeNull = 4;
// thisCannotBeNull = null; // compile error!
int? thisCouldBeNull = null;
Console.WriteLine(thisCouldBeNull);
Console.WriteLine(thisCouldBeNull.GetValueOrDefault());
thisCouldBeNull = 7;
Console.WriteLine(thisCouldBeNull);
Console.WriteLine(thisCouldBeNull.GetValueOrDefault());
#!markdown
## Checking for null
#!csharp
string authorName = null;
// the following throws a NullReferenceException
// int x = authorName.Length;
// instead of throwing an exception, null is assigned to y
int? y = authorName?.Length;
Console.WriteLine($"y is null: {y is null}");
// result will be 3 if authorName?.Length is null
var result = authorName?.Length ?? 3;
Console.WriteLine(result);
#!markdown
## Formatting using numbered positional arguments
#!csharp
using static System.Console;
#!csharp
int numberOfApples = 12;
decimal pricePerApple = 0.35M;
WriteLine(
format: "{0} apples costs {1:C}",
arg0: numberOfApples,
arg1: pricePerApple * numberOfApples);
string formatted = string.Format(
format: "{0} apples costs {1:C}",
arg0: numberOfApples,
arg1: pricePerApple * numberOfApples);
//WriteToFile(formatted); // writes the string into a file
#!markdown
## Formatting using interpolated strings
#!csharp
WriteLine($"{numberOfApples} apples costs {pricePerApple * numberOfApples:C}");
#!markdown
## Understanding format strings
#!csharp
string applesText = "Apples";
int applesCount = 1234;
string bananasText = "Bananas";
int bananasCount = 56789;
WriteLine(
format: "{0,-8} {1,6:N0}",
arg0: "Name",
arg1: "Count");
WriteLine(
format: "{0,-8} {1,6:N0}",
arg0: applesText,
arg1: applesCount);
WriteLine(
format: "{0,-8} {1,6:N0}",
arg0: bananasText,
arg1: bananasCount);
#!markdown
## Getting text input from the user
.NET Interactive notebooks do not support `ReadLine()` so in the following code we must set literal string values for the two variables.
#!csharp
Write("Type your first name and press ENTER: ");
string firstName = "Gary"; // cannot use Console.ReadLine()
Write("Type your age and press ENTER: ");
string age = "34"; // cannot use Console.ReadLine()
WriteLine(
$"Hello {firstName}, you look good for {age}.");
#!markdown
## Getting key input from the user
`Console.ReadKey()` does not work in a .NET notebook.
#!markdown
## Getting arguments
Arguments cannot be passed to a .NET notebook.
#!markdown
## Exercise 3 - Practice number sizes and ranges
#!csharp
WriteLine("--------------------------------------------------------------------------");
WriteLine("Type Byte(s) of memory Min Max");
WriteLine("--------------------------------------------------------------------------");
WriteLine($"sbyte {sizeof(sbyte),-4} {sbyte.MinValue,30} {sbyte.MaxValue,30}");
WriteLine($"byte {sizeof(byte),-4} {byte.MinValue,30} {byte.MaxValue,30}");
WriteLine($"short {sizeof(short),-4} {short.MinValue,30} {short.MaxValue,30}");
WriteLine($"ushort {sizeof(ushort),-4} {ushort.MinValue,30} {ushort.MaxValue,30}");
WriteLine($"int {sizeof(int),-4} {int.MinValue,30} {int.MaxValue,30}");
WriteLine($"uint {sizeof(uint),-4} {uint.MinValue,30} {uint.MaxValue,30}");
WriteLine($"long {sizeof(long),-4} {long.MinValue,30} {long.MaxValue,30}");
WriteLine($"ulong {sizeof(ulong),-4} {ulong.MinValue,30} {ulong.MaxValue,30}");
WriteLine($"float {sizeof(float),-4} {float.MinValue,30} {float.MaxValue,30}");
WriteLine($"double {sizeof(double),-4} {double.MinValue,30} {double.MaxValue,30}");
WriteLine($"decimal {sizeof(decimal),-4} {decimal.MinValue,30} {decimal.MaxValue,30}");
WriteLine("--------------------------------------------------------------------------");