-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
GenericWhereConstraints.cs
273 lines (231 loc) · 6.67 KB
/
GenericWhereConstraints.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using static keywords.UnmanagedExtensions;
namespace keywords
{
// <Snippet1>
public class AGenericClass<T> where T : IComparable<T> { }
// </Snippet1>
// <SNippet2>
public class UsingEnum<T> where T : System.Enum { }
public class UsingDelegate<T> where T : System.Delegate { }
public class Multicaster<T> where T : System.MulticastDelegate { }
// </Snippet2>
// <Snippet3>
class MyClass<T, U>
where T : class
where U : struct
{ }
// </Snippet3>
// <Snippet4>
class UnManagedWrapper<T>
where T : unmanaged
{ }
// </Snippet4>
// <Snippet5>
public class MyGenericClass<T> where T : IComparable<T>, new()
{
// The following line is not possible without new() constraint:
T item = new T();
}
// </Snippet5>
// <Snippet6>
public interface IMyInterface { }
namespace CodeExample
{
class Dictionary<TKey, TVal>
where TKey : IComparable<TKey>
where TVal : IMyInterface
{
public void Add(TKey key, TVal val) { }
}
}
// </Snippet6>
public class Container
{
// <Snippet7>
public void MyMethod<T>(T t) where T : IMyInterface { }
// </Snippet7>
// <Snippet8>
delegate T MyDelegate<T>() where T : new();
// </Snippet8>
}
// <Snippet9>
public class Employee
{
public Employee(string s, int i) => (Name, ID) = (s, i);
public string Name { get; set; }
public int ID { get; set; }
}
public class GenericList<T> where T : Employee
{
private class Node
{
public Node(T t) => (Next, Data) = (null, t);
public Node Next { get; set; }
public T Data { get; set; }
}
private Node head;
public void AddHead(T t)
{
Node n = new Node(t) { Next = head };
head = n;
}
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
public T FindFirstOccurrence(string s)
{
Node current = head;
T t = null;
while (current != null)
{
//The constraint enables access to the Name property.
if (current.Data.Name == s)
{
t = current.Data;
break;
}
else
{
current = current.Next;
}
}
return t;
}
}
// </Snippet9>
public interface IEmployee
{
}
// <Snippet10>
class EmployeeList<T> where T : Employee, IEmployee, System.IComparable<T>, new()
{
// ...
}
// </Snippet10>
// <Snippet12>
class Base { }
class Test<T, U>
where U : struct
where T : Base, new()
{ }
// </Snippet12>
// <Snippet13>
public class List<T>
{
public void Add<U>(List<U> items) where U : T {/*...*/}
}
// </Snippet13>
// <Snippet14>
//Type parameter V is used as a type constraint.
public class SampleClass<T, U, V> where T : V { }
// </Snippet14>
public static class UnmanagedExtensions
{
// <Snippet15>
unsafe public static byte[] ToByteArray<T>(this T argument) where T : unmanaged
{
var size = sizeof(T);
var result = new Byte[size];
Byte* p = (byte*)&argument;
for (var i = 0; i < size; i++)
result[i] = *p++;
return result;
}
// </Snippet15>
// <Snippet16>
public static TDelegate TypeSafeCombine<TDelegate>(this TDelegate source, TDelegate target)
where TDelegate : System.Delegate
=> Delegate.Combine(source, target) as TDelegate;
// </Snippet16>
// <Snippet18>
public static Dictionary<int, string> EnumNamedValues<T>() where T : System.Enum
{
var result = new Dictionary<int, string>();
var values = Enum.GetValues(typeof(T));
foreach (int item in values)
result.Add(item, Enum.GetName(typeof(T), item));
return result;
}
// </Snippet18>
}
public static class GenericWhereConstraints
{
public static void Examples()
{
TestStringEquality();
TestUnmanaged();
TestDelegateCombination();
TestEnumValues();
}
// <Snippet11>
public static void OpEqualsTest<T>(T s, T t) where T : class
{
System.Console.WriteLine(s == t);
}
private static void TestStringEquality()
{
string s1 = "target";
System.Text.StringBuilder sb = new System.Text.StringBuilder("target");
string s2 = sb.ToString();
OpEqualsTest<string>(s1, s2);
}
// </Snippet11>
public struct Point3D
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
}
private static void TestUnmanaged()
{
var thing = new Point3D { X = 1, Y = 2, Z = 3 };
var storage = thing.ToByteArray();
for (int i = 0; i < storage.Length; i++)
Console.Write($"{storage[i]:X2}, ");
Console.WriteLine();
}
private static void TestDelegateCombination()
{
// <Snippet17>
Action first = () => Console.WriteLine("this");
Action second = () => Console.WriteLine("that");
var combined = first.TypeSafeCombine(second);
combined();
Func<bool> test = () => true;
// Combine signature ensures combined delegates must
// have the same type.
//var badCombined = first.TypeSafeCombine(test);
// </Snippet17>
}
// <Snippet19>
enum Rainbow
{
Red,
Orange,
Yellow,
Green,
Blue,
Indigo,
Violet
}
// </Snippet19>
private static void TestEnumValues()
{
// <Snippet20>
var map = EnumNamedValues<Rainbow>();
foreach (var pair in map)
Console.WriteLine($"{pair.Key}:\t{pair.Value}");
// </Snippet20>
}
}
}