Skip to content

Commit 6008db8

Browse files
committed
Improving notes on generic types.
1 parent 25cc0fa commit 6008db8

4 files changed

Lines changed: 67 additions & 36 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Helper
2+
{
3+
// A generic way of reversing an array.
4+
public static T[] Reverse<T>(T[] arrayP)
5+
{
6+
T[] result = new T[arrayP.Length];
7+
int j = 0;
8+
for (int i = arrayP.Length - 1; i >= 0; i--)
9+
{
10+
result[j] = arrayP[i];
11+
j++;
12+
}
13+
return result;
14+
}
15+
16+
// A generic way of describing an array.
17+
18+
public static string Description<T>(T[] arrayP)
19+
{
20+
string returned = "";
21+
foreach (var element in arrayP)
22+
{
23+
returned += element + " ";
24+
}
25+
return returned;
26+
}
27+
// Done.
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main()
6+
{
7+
// Using Reverse:
8+
int[] array1 = { 0, 2, 3, 6 };
9+
int[] array1reversed = Helper.Reverse<int>(array1);
10+
11+
char[] array2 = { 'a', 'b', 'c' };
12+
char[] array2reversed = Helper.Reverse<char>(array2);
13+
14+
// Using Description:
15+
Console.WriteLine("Array 1:");
16+
Console.WriteLine(Helper.Description<int>(array1));
17+
Console.WriteLine("Array 1 reversed:");
18+
Console.WriteLine(
19+
Helper.Description<int>(array1reversed)
20+
);
21+
// Done.
22+
}
23+
}

source/lectures/misc/references.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ int a; // This code will not compile
250250
SetToRandom(ref a);
251251
```
252252

253-
Because C#'s compilation will return the error message "Use of unassigned local variable 'c'".
253+
Because C#'s compilation will return the error message "Use of unassigned local variable 'a'".
254254
Indeed, `SetToRandom` expects the argument to already holds a reference to a value, even if it has no use for it.
255255

256256
A better alternative is to use the `out` keyword:

source/lectures/oop/generic_types.md

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,19 @@ This is not very efficient, and it is error-prone.
4242

4343
## Generic Types
4444

45-
There is a tool in C# to avoid having to be *too* specific, and to be able to tell the compiler that the method will work "with some type", called [generic type parameter](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-type-parameters), using the keyword `T`.
45+
There is a tool in C# to avoid having to be *too* specific, and to be able to tell the compiler that the method will work "with some type", called [generic type parameter](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-type-parameters), using the notation `<T>`.
4646
In essence, `<T>` is affixed after the name of the method to signal that the method will additionally require to instantiate `T` with a particular type.
4747

4848
The previous method would become:
4949

50-
```
51-
public class Helper{
52-
public static T[] Reverse<T>(T[] arrayP)
53-
{
54-
T[] result = new T[arrayP.Length];
55-
int j = 0;
56-
for (int i = arrayP.Length - 1; i >= 0; i--)
57-
{
58-
result[j] = arrayP[i];
59-
j++;
60-
}
61-
return result;
62-
}
63-
}
50+
```{download="./code/projects/GenericType.zip"}
51+
!include`snippetStart="// A generic way of reversing an array.", snippetEnd="// A generic way of describing an array."` code/projects/GenericType/GenericType/Helper.cs
6452
```
6553

6654
where three occurrences of `int[]` were replaced by `T[]`, and `<T>` was additionally added between the name of the method and its parameters. This method is used as follows:
6755

68-
```
69-
int[] array1 = {0, 2, 3, 6};
70-
int[] array1reversed = Helper.Reverse<int>(array1);
71-
72-
char[] array2 = {'a', 'b', 'c'};
73-
char[] array2reversed = Helper.Reverse<char>(array2);
56+
```{download="./code/projects/GenericType.zip"}
57+
!include`snippetStart="// Using Reverse:", snippetEnd="// Using Description:"` code/projects/GenericType/GenericType/Program.cs
7458
```
7559

7660
In essence, `Reverse<int>` tells C# that `Reverse` will be used with `T` being `int` (not `int[]`, as the method uses `T[]` for its argument and return type).
@@ -101,28 +85,24 @@ Making the header generic is "easy", as we can use, as before:
10185
public static string Description<T>(T[] arrayP)
10286
```
10387

104-
but the body is problematic: what should be the type of the `element` variable in the header of the `foreach`? We cannot simply use `T`, but we can use *implicitly typed variable*.
88+
but the body is problematic: what should be the type of the `element` variable in the header of the `foreach`? We can simply use `T`, or we can use *implicitly typed variable*.
10589
This technique, that uses the keyword `var` essentially tells C# to … figure out the type of the variable.
10690
In that case, since C# knows the type of the array you are passing, it can easily infer the type of its elements.
10791

108-
We can then rewrite the previous method as follows:
92+
We can then rewrite the previous method as follows (using `T`, but simply replacing `T` with `var` gives another way):
10993

110-
```
111-
public static string Description<T>(T[] arrayP)
112-
{
113-
string returned = "";
114-
foreach (var element in arrayP)
115-
{
116-
returned += element + " ";
117-
}
118-
return returned;
119-
}
94+
```{download="./code/projects/GenericType.zip"}
95+
!include`snippetStart="// A generic way of describing an array.", snippetEnd="// Done."` code/projects/GenericType/GenericType/Helper.cs
12096
```
12197

12298
and use it with
12399

124-
```
125-
Console.WriteLine(Helper.Display<char>(array2);
100+
```{download="./code/projects/GenericType.zip"}
101+
!include`snippetStart="// Using Description:", snippetEnd="// Done."` code/projects/GenericType/GenericType/Program.cs
126102
```
127103

128104
for example.
105+
106+
## Comparing
107+
108+
Last but not least, C# does not know if `T` has access to the `==` operator, but it will always assume that a `Equals` method is used: refer to [how arrays are compared](./lectures/misc/references#comparing-arrays) for an example.

0 commit comments

Comments
 (0)