0% found this document useful (0 votes)
15 views19 pages

Understanding Generics in C#

Generics in C# provide a type-safe solution for writing reusable code without performance loss by allowing the specification of types at the time of method or class usage. They use a placeholder, typically named T, which is replaced by the actual type during compilation. Constraints can be applied to ensure that only specific types are used, enhancing the flexibility and safety of the code.

Uploaded by

mehimi1976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views19 pages

Understanding Generics in C#

Generics in C# provide a type-safe solution for writing reusable code without performance loss by allowing the specification of types at the time of method or class usage. They use a placeholder, typically named T, which is replaced by the actual type during compilation. Constraints can be applied to ensure that only specific types are used, enhancing the flexibility and safety of the code.

Uploaded by

mehimi1976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Generics in C#

Dikkat kya hai?

public static object Add(object a, object b)


public
{ static int Add(int a, int b) => a + b;
int p = (int) a;
public static
int q double
= (int) Add(double
b; a, double b) => a + b;
return p+q;
public
} static float Add(float a, float b) => a + b;
Code Repetition,
public static long Add(long a, long b) => a + b; DUH.
We need a type-safe solution
for writing reusable code
which does not impede the
performance.
How did Generics solve this problem?

1. Generics allow you to specify the type of a method or class when you use it.
2. Their definitions are written using a custom type, often named as T.
3. At the compile time, this <T> is replaced by the type that is asked for.

Example :

public static T Add(T a, T b) => a+b;


Examples where we have used Generics
Examples where we have used Generics

Similar examples in CacheManager, ILogger, etc.


Examples where we have used Generics
Examples where we could have used Generics
Examples where we could have used Generics
Examples where we could have used Generics
Examples where we could have used Generics
Sorry for misleading you,
but it was necessary for the plot.
Constraints

1. Generics secretly still loves System.Object

2. We need to tell the compiler that we are only going to use numbers (a
specific type)

3. But then compiler will take our word to the grave


Constraints
and the moment we use that Generic for something else, we get
an error

Compilation Error
Constraints

We can use constraints to -


1. only use non-nullable value types like struct
2. only use reference types
3. only use types which are derived from certain class, or implemented
using a certain interface
4. only use nullable or non-nullable types
5. only use type which have public parameterless constructor
6. only use unmanaged type
DOs & DONTs

● Use wherever you can, especially while creating libraries, utilities and
implementing design patterns

● Use the commonly used names for Generic types like T, U, V, and so on.

● In certain cases, it makes more sense to ditch them and go for more
meaningful versions of them like TKey, TValue. This helps in
differentiating the generic code.
DOs & DONTs

Generics istemaal karna acchi baat hai

Lekin jyada generic banana acchi baat nahi hai


You can refer to :

https://ct-tech.atlassian.net/wiki/spaces/engineering/pages/
438566956/Generics

You might also like