You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/lectures/oop/generic_types.md
+15-35Lines changed: 15 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,35 +42,19 @@ This is not very efficient, and it is error-prone.
42
42
43
43
## Generic Types
44
44
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>`.
46
46
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.
47
47
48
48
The previous method would become:
49
49
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
64
52
```
65
53
66
54
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:
!include`snippetStart="// Using Reverse:", snippetEnd="// Using Description:"` code/projects/GenericType/GenericType/Program.cs
74
58
```
75
59
76
60
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:
101
85
public static string Description<T>(T[] arrayP)
102
86
```
103
87
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*.
105
89
This technique, that uses the keyword `var` essentially tells C# to … figure out the type of the variable.
106
90
In that case, since C# knows the type of the array you are passing, it can easily infer the type of its elements.
107
91
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):
109
93
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
120
96
```
121
97
122
98
and use it with
123
99
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
126
102
```
127
103
128
104
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