0% found this document useful (0 votes)
0 views

Generic and Generic Constant in C#

The document explains generics in C#, highlighting their purpose as flexible code that can operate on any data type, enhancing reusability and type safety. It also discusses generic constants, which are fixed values used within generics, and the limitations of using 'const' in generics due to type unknowns at compile time. Alternatives like static readonly fields are suggested for achieving similar functionality in generics.

Uploaded by

fatimaaftabfsd
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)
0 views

Generic and Generic Constant in C#

The document explains generics in C#, highlighting their purpose as flexible code that can operate on any data type, enhancing reusability and type safety. It also discusses generic constants, which are fixed values used within generics, and the limitations of using 'const' in generics due to type unknowns at compile time. Alternatives like static readonly fields are suggested for achieving similar functionality in generics.

Uploaded by

fatimaaftabfsd
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/ 18

Generic and Generic

Constant in C#
Group 6: (Noor Fatima, Uswa Asif, Kiran Farooq)
Generic and Generic
Constant, Purposes
(Noor Fatima | 23179 | ADP (CS) MA )
Generic
A generic is like a blueprint that can work with any type of
data.
Explaination
Instead of making separate code for each type (like for
int, float, or string), you write one generic code that
works for all types.
It helps to make the code reusable, flexible, and safe.
Example
Example in simple words: Imagine you have one box,
and you can put anything in it — a book, a toy, or clothes.
Similarly, in programming, a generic can hold or work
with any data type.
Generic constant
A generic constant is a fixed value used inside a
generic.
It doesn't change, and it can be used across different
types while keeping the value same.
It helps to make generics more powerful by adding fixed
values that apply for all types.
Example
Example in simple words: Think of a pizza recipe that
always needs 2 cups of flour — no matter if you are
making small, medium, or large pizza sizes.
Here, 2 cups of flour is like a generic constant — it
stays the same across different cases.
Summary:
Generic = flexible code for any type.
Generic Constant = fixed value used inside a generic
code.
Purpose
To define a fixed, unchangeable value that works for
any data type.
To ensure consistency and readability in generic code.
To avoid magic numbers or hardcoded values when
working with generics.
Understanding Generic
Classes, Methods, and
Constants
(Uswa Asif | 23178 | ADP (CS) MA )
Generic Class in C#
• Defines a type parameter in the class declaration.
• Operates on any data type without losing type
safety.
Example:
class Box<T>
{
private T value;

public Box(T value)


{
this.value = value;
}

public void Display()


{
Console.WriteLine("Value: " +
value);
}
}
• T can be int, string, float, etc., based on the object
created.
Generic Method in C#
• Works with any data type.
• Independent of specific
types.

Example:

public void DisplayData<T>(T data)


{
Console.WriteLine(data);
}

• Accepts any type as a parameter.


Why const is Not Allowed in
Generics?
Main Reasons:

• const values must be known at compile time.


• The type parameter T in a generic class is only known at
runtime.
• Therefore, const T is not possible in generics.
Generic Constants in C#
Problem:

Cannot declare const inside a generic


class.

Example that would NOT work:

class Test<T>
{
public const T DefaultValue = default(T); // Error!
}

❌ Error: Type T is unknown at compile time.


Alternative to Const in Generics
• Use static readonly fields.
• Use default(T) to provide default
values.
Example:

class Test<T>
{
public static readonly T DefaultValue = default(T);
}

default(T) provides:
• 0 for numbers,
• false for bool,
• null for reference types.
Advantages and
Conclusion
(Kiran Farooq | 23177 | ADP (CS) MA )
Advantages/Benefits of Generic in C#
• Type Safety: Ensures you're using the correct data type.
• Reusability: Write code once, use it with many types (e.g., int, string, custom classes).
• No Casting: No need to cast objects, reducing errors.
• Better Performance: Avoids boxing/unboxing overhead.
• Code Readability: Clearly communicates the type being used.
Conclusion of Generic in C#
Generics help you write flexible and reusable code that works with any data type. They
provide type safety, reducing errors and improving performance.
Generic VS Constant
You can't use constants (const) directly in generics because the type is unknown until runtime.
Instead, use readonly fields or default(T) to achieve similar results.

You might also like