forked from markjprice/cs11dotnet7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter06.dib
860 lines (622 loc) · 15.4 KB
/
Chapter06.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
#!markdown
# Chapter 6 - Implementing Interfaces and Inheriting Classes
#!csharp
using static System.Console;
#!csharp
public class PersonException : Exception
{
public PersonException() : base() { }
public PersonException(string message) : base(message) { }
public PersonException(string message, Exception innerException)
: base(message, innerException) { }
}
#!csharp
#nullable enable // required in notebook cells that use nullability
public class Person : object, IComparable<Person>
{
// fields
public string? Name; // ? allows null
public DateTime DateOfBirth;
public List<Person> Children = new(); // C# 9 or later
// methods
public void WriteToConsole()
{
WriteLine($"{Name} was born on a {DateOfBirth:dddd}.");
}
// static method to "multiply"
public static Person Procreate(Person p1, Person p2)
{
Person baby = new()
{
Name = $"Baby of {p1.Name} and {p2.Name}"
};
p1.Children.Add(baby);
p2.Children.Add(baby);
return baby;
}
// instance method to "multiply"
public Person ProcreateWith(Person partner)
{
return Procreate(this, partner);
}
// operator to "multiply"
public static Person operator *(Person p1, Person p2)
{
return Person.Procreate(p1, p2);
}
// method with a local function
public static int Factorial(int number)
{
if (number < 0)
{
throw new ArgumentException(
$"{nameof(number)} cannot be less than zero.");
}
return localFactorial(number);
int localFactorial(int localNumber) // local function
{
if (localNumber < 1) return 1;
return localNumber * localFactorial(localNumber - 1);
}
}
// delegate field
public event EventHandler? Shout;
// data field
public int AngerLevel;
// method
public void Poke()
{
AngerLevel++;
if (AngerLevel >= 3)
{
// if something is listening...
if (Shout != null)
{
// ...then call the delegate
Shout(this, EventArgs.Empty);
}
}
}
public int CompareTo(Person? other)
{
if (Name is null) return 0;
return Name.CompareTo(other?.Name);
}
// overridden methods
public override string ToString()
{
return $"{Name} is a {base.ToString()}";
}
public void TimeTravel(DateTime when)
{
if (when <= DateOfBirth)
{
throw new PersonException("If you travel back in time to a date earlier than your own birth, then the universe will explode!");
}
else
{
WriteLine($"Welcome to {when:yyyy}!");
}
}
// end of class
}
#!markdown
# Implementing functionality using methods
#!csharp
Person harry = new() { Name = "Harry" };
Person mary = new() { Name = "Mary" };
Person jill = new() { Name = "Jill" };
// call instance method
Person baby1 = mary.ProcreateWith(harry);
baby1.Name = "Gary";
// call static method
Person baby2 = Person.Procreate(harry, jill);
// call an operator
Person baby3 = harry * mary;
WriteLine($"{harry.Name} has {harry.Children.Count} children.");
WriteLine($"{mary.Name} has {mary.Children.Count} children.");
WriteLine($"{jill.Name} has {jill.Children.Count} children.");
WriteLine(
format: "{0}'s first child is named \"{1}\".",
arg0: harry.Name,
arg1: harry.Children[0].Name);
#!markdown
## Implementing functionality using local functions
#!csharp
WriteLine($"5! is {Person.Factorial(5)}");
#!markdown
# Raising and handling events
#!markdown
## Defining and handling delegates
#!csharp
#nullable enable
static void Harry_Shout(object? sender, EventArgs e)
{
if (sender is null) return;
Person p = (Person)sender;
WriteLine($"{p.Name} is this angry: {p.AngerLevel}.");
}
#!csharp
harry.Shout += Harry_Shout;
#!csharp
harry.Poke();
harry.Poke();
harry.Poke();
harry.Poke();
#!markdown
# Making types safely reusable with generics
#!markdown
## Working with non-generic types
#!csharp
// non-generic lookup collection
System.Collections.Hashtable lookupObject = new();
lookupObject.Add(key: 1, value: "Alpha");
lookupObject.Add(key: 2, value: "Beta");
lookupObject.Add(key: 3, value: "Gamma");
lookupObject.Add(key: harry, value: "Delta");
int key = 2; // lookup the value that has 2 as its key
WriteLine(format: "Key {0} has value: {1}",
arg0: key,
arg1: lookupObject[key]);
// lookup the value that has harry as its key
WriteLine(format: "Key {0} has value: {1}",
arg0: harry,
arg1: lookupObject[harry]);
#!markdown
## Working with generic types
#!csharp
// generic lookup collection
Dictionary<int, string> lookupIntString = new();
lookupIntString.Add(key: 1, value: "Alpha");
lookupIntString.Add(key: 2, value: "Beta");
lookupIntString.Add(key: 3, value: "Gamma");
lookupIntString.Add(key: 4, value: "Delta");
key = 3;
WriteLine(format: "Key {0} has value: {1}",
arg0: key,
arg1: lookupIntString[key]);
#!markdown
# Implementing interfaces
#!markdown
## Comparing objects when sorting
#!csharp
Person[] people =
{
new() { Name = "Simon" },
new() { Name = "Jenny" },
new() { Name = "Adam" },
new() { Name = "Richard" }
};
WriteLine("Initial list of people:");
foreach (Person p in people)
{
WriteLine($" {p.Name}");
}
WriteLine("Use Person's IComparable implementation to sort:");
Array.Sort(people);
foreach (Person p in people)
{
WriteLine($" {p.Name}");
}
#!markdown
## Comparing objects using a separate class
#!csharp
#nullable enable
public class PersonComparer : IComparer<Person>
{
public int Compare(Person? x, Person? y)
{
if (x is null || y is null)
{
return 0;
}
// Compare the Name lengths...
int result = x.Name.Length.CompareTo(y.Name.Length);
// ...if they are equal...
if (result == 0)
{
// ...then compare by the Names...
return x.Name.CompareTo(y.Name);
}
else // result will be -1 or 1
{
// ...otherwise compare by the lengths.
return result;
}
}
}
#!csharp
WriteLine("Use PersonComparer's IComparer implementation to sort:");
Array.Sort(people, new PersonComparer());
foreach (Person p in people)
{
WriteLine($" {p.Name}");
}
#!markdown
## Implicit and explicit interface implementations
#!csharp
public interface IGamePlayer
{
void Lose();
}
public interface IKeyHolder
{
void Lose();
}
public class Human : IGamePlayer, IKeyHolder
{
public void Lose() // implicit implementation
{
// implement losing a key
WriteLine("Lost my key!");
}
void IGamePlayer.Lose() // explicit implementation
{
// implement losing a game
WriteLine("Lost the game!");
}
}
// calling implicit and explicit implementations of Lose
Human p = new();
p.Lose(); // calls implicit implementation of losing a key
((IGamePlayer)p).Lose(); // calls explicit implementation of losing a game
IGamePlayer player = p as IGamePlayer;
player.Lose(); // calls explicit implementation of losing a game
#!markdown
## Defining interfaces with default implementations
Note that the code cell below executes successfully despite the `DvdPlayer` class not implementing `Stop`.
#!csharp
public interface IPlayable
{
void Play();
void Pause();
void Stop() // default interface implementation
{
WriteLine("Default implementation of Stop.");
}
}
public class DvdPlayer : IPlayable
{
public void Pause()
{
WriteLine("DVD player is pausing.");
}
public void Play()
{
WriteLine("DVD player is playing.");
}
}
#!markdown
# Managing memory with reference and value types
#!markdown
## How reference and value types are stored in memory
#!csharp
int number1 = 49;
long number2 = 12;
System.Drawing.Point location = new(x: 4, y: 5);
Person kevin = new() { Name = "Kevin",
DateOfBirth = new(year: 1988, month: 9, day: 23) };
Person sally;
#!markdown
## Equality of types
#!csharp
int a = 3;
int b = 3;
WriteLine($"a == b: {(a == b)}"); // true
#!csharp
Person2 a = new() { Name = "Kevin" };
Person2 b = new() { Name = "Kevin" };
WriteLine($"a == b: {(a == b)}"); // false
#!csharp
Person2 a = new() { Name = "Kevin" };
Person2 b = a;
WriteLine($"a == b: {(a == b)}"); // true
#!csharp
string a = "Kevin";
string b = "Kevin";
WriteLine($"a == b: {(a == b)}"); // true
#!markdown
# Defining struct types
#!csharp
public struct DisplacementVector
{
public int X;
public int Y;
public DisplacementVector(int initialX, int initialY)
{
X = initialX;
Y = initialY;
}
public static DisplacementVector operator +(
DisplacementVector vector1,
DisplacementVector vector2)
{
return new(
vector1.X + vector2.X,
vector1.Y + vector2.Y);
}
}
#!csharp
DisplacementVector dv1 = new(3, 5);
DisplacementVector dv2 = new(-2, 7);
DisplacementVector dv3 = dv1 + dv2;
WriteLine($"({dv1.X}, {dv1.Y}) + ({dv2.X}, {dv2.Y}) = ({dv3.X}, {dv3.Y})");
#!markdown
## Working with record struct types
#!csharp
public record struct DisplacementVector(int X, int Y);
#!markdown
## Releasing unmanaged resources
#!csharp
public class Animal
{
public Animal() // constructor
{
// allocate any unmanaged resources
}
~Animal() // Finalizer aka destructor
{
// deallocate any unmanaged resources
}
}
#!csharp
public class Animal : IDisposable
{
public Animal()
{
// allocate unmanaged resource
}
~Animal() // Finalizer
{
Dispose(false);
}
bool disposed = false; // have resources been released?
public void Dispose()
{
Dispose(true);
// tell garbage collector it does not need to call the finalizer
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed) return;
// deallocate the *unmanaged* resource
// ...
if (disposing)
{
// deallocate any other *managed* resources
// ...
}
disposed = true;
}
}
#!csharp
using (Animal a = new())
{
// code that uses the Animal instance
}
#!csharp
Animal a = new();
try
{
// code that uses the Animal instance
}
finally
{
if (a != null) a.Dispose();
}
#!markdown
# Working with null values
#!csharp
int thisCannotBeNull = 4;
//thisCannotBeNull = null; // compile error!
int? thisCouldBeNull = null;
WriteLine(thisCouldBeNull);
WriteLine(thisCouldBeNull.GetValueOrDefault());
thisCouldBeNull = 7;
WriteLine(thisCouldBeNull);
WriteLine(thisCouldBeNull.GetValueOrDefault());
#!markdown
## Declaring non-nullable variables and parameters
#!csharp
#nullable enable
class Address
{
public string? Building;
public string Street = string.Empty;
public string City = string.Empty;
public string Region = string.Empty;
}
Address address = new();
address.Building = null;
address.Street = null;
address.City = "London";
address.Region = null;
#!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;
if (!y.HasValue) WriteLine("y is null");
// result will be 3 if authorName?.Length is null
int result = authorName?.Length ?? 3;
Console.WriteLine(result);
#!markdown
# Inheriting from classes
#!csharp
#nullable enable
public class Employee : Person
{
public string? EmployeeCode { get; set; }
public DateTime HireDate { get; set; }
public new void WriteToConsole()
{
WriteLine(format:
"{0} was born on {1:dd/MM/yy} and hired on {2:dd/MM/yy}",
arg0: Name,
arg1: DateOfBirth,
arg2: HireDate);
}
public override string ToString()
{
return $"{Name}'s code is {EmployeeCode}";
}
}
#!csharp
Employee john = new()
{
Name = "John Jones",
DateOfBirth = new(year: 1990, month: 7, day: 28)
};
john.WriteToConsole();
#!markdown
## Extending classes to add functionality
#!csharp
john.EmployeeCode = "JJ001";
john.HireDate = new(year: 2014, month: 11, day: 23);
WriteLine($"{john.Name} was hired on {john.HireDate:dd/MM/yy}");
#!markdown
## Overriding members
#!csharp
WriteLine(john.ToString());
#!markdown
## Inheriting from abstract classes
#!csharp
public interface INoImplementation // C# 1.0 and later
{
void Alpha(); // must be implemented by derived type
}
public interface ISomeImplementation // C# 8.0 and later
{
void Alpha(); // must be implemented by derived type
void Beta()
{
// default implementation; can be overridden
}
}
public abstract class PartiallyImplemented // C# 1.0 and later
{
public abstract void Gamma(); // must be implemented by derived type
public virtual void Delta() // can be overridden
{
// implementation
}
}
public class FullyImplemented : PartiallyImplemented, ISomeImplementation
{
public void Alpha()
{
// implementation
}
public override void Gamma()
{
// implementation
}
}
// you can only instantiate the fully implemented class
FullyImplemented a = new();
// all the other types give compile errors
PartiallyImplemented b = new(); // compile error!
ISomeImplementation c = new(); // compile error!
INoImplementation d = new(); // compile error!
#!markdown
## Preventing inheritance and overriding
You can prevent another developer from inheriting from your class by applying the `sealed`
keyword to its definition.
#!csharp
public sealed class ScroogeMcDuck
{
}
#!markdown
You can prevent someone from further overriding a `virtual` method in your class by applying
the `sealed` keyword to the method. You can only seal an overridden method.
#!csharp
public class Singer
{
// virtual allows this method to be overridden
public virtual void Sing()
{
WriteLine("Singing...");
}
}
public class LadyGaga : Singer
{
// sealed prevents overriding the method in subclasses
public sealed override void Sing()
{
WriteLine("Singing with style...");
}
}
#!markdown
## Understanding polymorphism
#!csharp
Employee aliceInEmployee = new()
{ Name = "Alice", EmployeeCode = "AA123" };
Person aliceInPerson = aliceInEmployee;
aliceInEmployee.WriteToConsole();
aliceInPerson.WriteToConsole();
WriteLine(aliceInEmployee.ToString());
WriteLine(aliceInPerson.ToString());
#!markdown
## Casting within inheritance hierarchies
#!csharp
Employee explicitAlice = aliceInPerson;
#!csharp
if (aliceInPerson is Employee)
{
WriteLine($"{nameof(aliceInPerson)} IS an Employee");
Employee explicitAlice = (Employee)aliceInPerson;
// safely do something with explicitAlice
}
#!csharp
#nullable enable
Employee? aliceAsEmployee = aliceInPerson as Employee; // could be null
if (aliceAsEmployee != null)
{
WriteLine($"{nameof(aliceInPerson)} AS an Employee");
// safely do something with aliceAsEmployee
}
#!markdown
# Inheriting and extending .NET types
#!csharp
try
{
john.TimeTravel(when: new(1999, 12, 31));
john.TimeTravel(when: new(1950, 12, 25));
}
catch (PersonException ex)
{
WriteLine(ex.Message);
}
#!markdown
## Using static methods to reuse functionality
#!csharp
using System.Text.RegularExpressions;
#!csharp
public class StringExtensions
{
public static bool IsValidEmail(string input)
{
// use simple regular expression to check
// that the input string is a valid email
return Regex.IsMatch(input,
@"[a-zA-Z0-9\.-_]+@[a-zA-Z0-9\.-_]+");
}
}
#!csharp
string email1 = "[email protected]";
string email2 = "ian&test.com";
WriteLine("{0} is a valid e-mail address: {1}",
arg0: email1,
arg1: StringExtensions.IsValidEmail(email1));
WriteLine("{0} is a valid e-mail address: {1}",
arg0: email2,
arg1: StringExtensions.IsValidEmail(email2));
#!markdown
Extension methods must be defined in a top-level static method so they cannot be defined in a notebook.
#!markdown
# Using an analyzer to write better code
You cannot run code analyzers on notebook code.