قالب وردپرس درنا توس
Home / Tips and Tricks / How does T work in C #? How to use generic type parameters – CloudSavvy IT

How does T work in C #? How to use generic type parameters – CloudSavvy IT



C #

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 Work?

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 and List; instead, there is a definition for List.

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.

Pass type parameters using <T> parentheses-syntax.  “width =” 458 “height =” 350 “onload =” pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);  “onerror =” this.onerror = null;  pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this);  “</p>
<p>To put something in <code><></code> in parentheses you can use that name instead of a valid type, anywhere in the definition of the class or method that uses that parameter.  If you imagine one <code>GenericList<int></code>, wherever you write <code>int</code> as a type you would write instead <code>T</code>and leave it up to the user to tell this class which type to use.</p>
<p>It really is that simple.  For one <code>GenericList<string></code>the code above is functionally equivalent to writing the following, but it should be noted that this definition does not include <code>GenericList<string></code>, because everything in parentheses is type parameters, and primitives cannot be used as type names.  For a class that does not use any parameters, like this one, it is defined as usual without parentheses.</p>
<p><img class=

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.

Bad naming practices.
This is bad naming practice. Do not do this.

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.

Use generic methods in methods.

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.

Use type parameters in the actual parameters of the function

They are especially useful for delegates, as you can use them to accept functions with variable parameters.

Write restrictions

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.

    Typical polymorphism

You can see that if we try to use this feature with something that is not a fruit, the compiler will shout at you.

    If you use this feature with something that is not a fruit, the compiler will comment

In addition to simple inheritance, there are some more useful limitations:

  • where T : InterfaceName – like T : ClassName but 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 : notnull the 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 T must be or derive from the type argument TOther.

You can specify multiple constraints in a comma-separated list.


Source link