METHODS OF GENERIC
Microsoft introduced generics to the C# module with edition 2.0 of the .NET framework. In an early article, I described the creation of generic types. These are classes and structures that impact with accumulation types that are undefinable within the identify itself and exclusive famous when the types are instantiated. If you are newborn to generics, you should feature the article, “Generic Types”, before continuing.
Generic methods ingest a kindred structure to generic types. They earmark individualist methods to be created that impact with types that are circumscribed exclusive when the method is called. A generic method includes digit or more type parameters in its signature. Those identify parameters crapper then be utilised for the convey continuance for the method and in its parameters. This allows the methods to be reused with whatever assorted types.
Generic methods deal whatever structure and functionality with generic classes and structures. However, it is not needed to create a generic method within a generic type. If you do add a generic method to a generic type, you should not ingest replicate identify constant obloquy in a method and its containing class. If you do, the method’s identify constant module conceal the containing type’s and a warning module be generated.
The structure for a generic method’s mode is as follows:
The most essential conception of the above structure is the type-parameters element. Here you take digit or more identify parameters in a comma-separated list. If desired, those types crapper also be utilised for the convey continuance and whatever or every of the method’s parameters.
The mass distribution cipher shows a ultimate generic method with a azygos identify parameter, titled “T”. In this case, the BuildList<T> method accepts a sort of objects of an undefinable identify and returns a generic List of the aforementioned type. In real-world applications this method serves no purpose. However, it is multipurpose for our demonstration.
List<T> BuildList<T>(params T[] items)
{
convey newborn List<T>(items);
}
When you call a generic method, you crapper take the types to be practical in locate of the identify parameters by insertion them between seek brackets (<, >). The mass call explicitly states that the method is to be utilised with strings.
You crapper also call generic a method without specifying the types that it module use. In the mass case, the items passed to the method are every integers. This allows the programme to derive the identify automatically. In whatever cases identify illation is not doable and the identify parameters staleness be provided.
Using Generic Constraints
Type constraints crapper be practical to individualist methods as they are with generic types. Derivation constraints, choice creator constraints and continuance / meaning identify constraints are every valid. The structure is same to that of generic types with the where keyword existence additional to the modify of the method signature.
In the distribution cipher below, a confinement specifies that T staleness be a continuance type. This module preclude the cipher from assembling because of the existing call that specifies the ingest of strings, which are meaning types.
public List<T> BuildList<T>(params T[] items) where T : struct
{
convey newborn List<T>(items);
}