Generics In C#
Omar Betar
Java Developer| Microservice Development. Based on Riyadh, Saudi Arabia, With +5 Years Experience In Backend and ERP System
In this article, we will try to answer those questions:
1- What is Generics?
2- Why Using Generics?
3-How to declare Generics?
4- what are Generics Types?
5-Advantage of using Generics over Object?
What is Generics?
-Generics were added to version 2.0 of the C# language.
-Generics introduces the concept of type parameters to .NET, which makes it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.
-Generic is a placeholder for a specific type that a client specifies when they create an instance of the generic type.
-Generic is a class that allows the user to define classes and methods with the placeholder.?
Why Using Generics?
-Generics provide type safety without the overhead of multiple implementations.
-Generics eliminates boxing and unboxing.
-There is no need to write code to test for the correct data type because it is enforced at compile time. The need for typecasting and the possibility of run-time errors are reduced.
-Generic collection types generally perform better for storing and manipulating value types because there is no need to box the value types.
-Generic delegates enable type-safe callbacks without the need to create multiple delegate classes.
- You can create your own generic interfaces, classes, methods, events, and delegates.
- You may get information on the types used in a generic data type at run-time by means of reflection.
How to declare Generics?
-A generic type is declared by specifying a type parameter in angle brackets after a type name, e.g.?TypeName<T>?where?T?is a type parameter.
class GenericClass<T>
{
public T GenericAtt{ get; set; }
}
What Are Generics Types?
-Generic Method:
A method declared with the type parameters for its return type or parameters
static void Main(strings[] args){
// here we can forced method to accept specific DataType.
Print<int>(1);
// output= 1
Print("Hello World");
// output= Hellow World
Print('F');
//output= F
Console.ReadKey();
}
static void Print<T> (T value) {
Console.WriteLine(value);
}
-Generics Class
-A generic class increases the reusability. The more type parameters mean more reusable it becomes. However, too much generalization makes code difficult to understand and maintain.
领英推荐
-You can create an instance of generic classes by specifying an actual type in angle brackets. The following creates an instance of the generic class?DataStore.
class GenericClass<T>
{
public T GenericAtt{ get; set; }
}
//declering a generic class
GenericClass<string> genericClass = new GenericClass<string>();
-Using Generics with delegates
you can define generic delegate with type parameters.
?delegate T genericDelagate<T>(T value);
static void main(string[] args)
{
?genericDelagate<string> genericDelagate =
new genericDelagate<string>(getvalues);
? Console.WriteLine(genericDelagate("Hello World"));
}
static T getvalues<T>(T value)
? ? {
? ? ? ? return value;
? ? }
-Generic Constraints
Constraints are used in Generics to restrict the types that can be substituted for type parameters.
public class GenericClass<T> where T: class{
public void genericMethod<X> () where C: string{}
}
Advantage of using Generics over Object?
-Object is more generally, defined as any polymorphic behavior.
-Object Effects Reuasabilty, TypeSafety, and Efficiency.
-Cannnot avoiding Boxing and Unboxing.
static void Main(strings[] args){
Print("Hello World");
// output= Hellow World
Print('F');
//output= F
Console.ReadKey();
}
static void Print (Object value) {
Console.WriteLine(value);
} {
Resources
.
3 年Keep going my dear ??