IC # often uses the “T” parameter to define functions that take any type. They are used to write generic classes and methods that can work with all types of data, while maintaining strict type certainty. We discuss how they work and how to use them.
How does it
The “T” variable that you have probably seen in some method definitions is called a Generic type parameter, or simply just a “generic”;. Generic methods that use T can be used with any type, making it easy to define classes and methods that do not care about the data they handle but want to preserve it.
For example, collections use generics so that they can handle what the user throws at it. There is no other definition for
List; instead, there is a definition for
In practice, it looks like the following. Even if this is a class and not a method, you can still pass type parameters using
parentheses syntax. Then, wherever you need to refer to something of this type, you simply replace the parameter instead.
You can actually name this T-variable anything, although it is common to at least start it with “T.” If you have a function that needs arguments of several kinds, you can name them differently, such as “TOutput” or “TInput.” This is widely used in delegate definitions and in dictionaries where you have TKey and TValue.
Of course, you can also use generics in methods, as well as interfaces and delegates. They work in the same way and you can even send the type parameter as a type parameter to another function.
In particular, however, you can use the type parameter in the actual parameters of the function. You still need to insert it
<> parentheses, otherwise it is just an invalid type, but you can use the parameter anywhere in the definition of this method.
They are especially useful for delegates, as you can use them to accept functions with variable parameters.
Generics is good, but it can cause some problems when the feature has to take all the possible types you throw at it. Sometimes it is best to set some restrictions on use.
This is done with
where T : syntax. The simplest form of this is
where T : ClassName, which ensures that the T-parameter must be or derive from the specified type
ClassName. This enables typical polymorphism, such as this feature that takes all kinds of fruit, and returns one
List, rather than one
List, which would be technically correct but loses valuable type information.
You can see that if we try to use this feature with something that is not a fruit, the compiler will shout at you.
In addition to simple inheritance, there are some more useful limitations:
where T : InterfaceName– like
T : ClassNamebut ensures that the type argument implements the specified interface.
where T : class– ensures that the type argument is a reference type.
where T : struct– ensures that the type argument is a non-invalid value type.
where T : notnullthe type argument must be an invalid type.
where T : new()the type argument must be able to be constructed without parameters.
where T : TOther– the type argument
Tmust be or derive from the type argument
You can specify multiple constraints in a comma-separated list.