1.

What are Generics in C#?

Answer»

In C# collections, defining any kind of object is termed okay which compromises C#’s basic rule of type-safety. Therefore, generics were INCLUDED to type-safe the code by allowing re-use of the data processing ALGORITHMS. Generics in C# mean not LINKED to any specific data type. Generics reduce the load of using boxing, unboxing, and typecasting OBJECTS. Generics are always defined INSIDE angular brackets <>. To create a generic class, this syntax is used:

GenericList<float> list1 = new GenericList<float>();GenericList<Features> list2 = new GenericList<Features>();GenericList<Struct> list3 = new GenericList<Struct>();

Here, GenericList<float> is a generic class. In each of these instances of GenericList<T>, every occurrence of T in the class is substituted at run time with the type argument. By substituting the T, we have created three different type-safe using the same class. 



Discussion

No Comment Found