C# Generics - Computer Science and Engineering Department

BhuvaneswaranB1 7 views 20 slides Sep 03, 2024
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

C# Generics - Computer Science and Engineering Department


Slide Content

B.Bhuvaneswaran, AP (SG) / CSE
9791519152
[email protected]
C# and .NET Programming
Generics

Generics Rajalakshmi Engineering College 2
Introduction
▪Generic class is a class, which contains one or more "type
parameters".
▪You must pass any data type (standard data type / structure /
class), while creating object for the generic class.

Generics Rajalakshmi Engineering College 3
Generic Class - Example
class ClassName<T>
{
public T FieldName;
}

Generics Rajalakshmi Engineering College 4
Object of Generic Class - Example
▪ClassName<int> referenceVariable = new ClassName<int> ( );

Generics Rajalakshmi Engineering College 5
Explanation
▪The same field may belong to different data types, w.r.t. different objects of the
same class.
▪You will decide the data type of the field, while creating the object, rather than
while creating field in the class.
▪It helps you in code reuse, performance and type-safety.
▪You can create your own generic-classes, generic-methods, generic-interfaces
and generic-delegates.
▪You can create generic collection classes. The .NET framework class library
contains many new generic collection classes in System.Collections.Generic
namespace.
▪The generic type parameter (T) acts as "temporary data type", which represents
the actual data type, provided by the user, while creating object.
▪You can have multiple "generic type parameters" in the same class (for use for
different fields.
▪Generics are introduced in C# 2.0.

Generics Rajalakshmi Engineering College 6
Generic Classes-Program
//Generic class
public class User<T1>
{
//generic field
public T1 RegistrationStatus;
}
class Program
{
static void Main()
{
//create object of generic class
User<int> user1 = new User<int>();
User<bool> user2 = new User<bool>();
//set value into generic field
user1.RegistrationStatus = 1234;
user2.RegistrationStatus = false;
System.Console.WriteLine (user1.RegistrationStatus);
System.Console.WriteLine (user2.RegistrationStatus);
System.Console.ReadKey();
}
}

Generics Rajalakshmi Engineering College 7
Multiple Generic Parameters-Program
//Generic class
public class User<T1, T2>
{
//generic field
public T1 RegistrationStatus;
//another generic field
public T2 Age;
}
class Program
{
static void Main()
{
//create object of generic class
User<int, int> user1 = new User<int, int>();
User<bool, string> user2 = new User<bool, string>();
//set value into generic field
user1.RegistrationStatus = 1234;
user2.RegistrationStatus = false;
user1.Age = 22;
user2.Age = "35 - 40";
System.Console.WriteLine(user1.RegistrationStatus);
System.Console.WriteLine(user2.RegistrationStatus);
System.Console.WriteLine(user1.Age);
System.Console.WriteLine(user2.Age);
System.Console.ReadKey();
}
}

Generics Rajalakshmi Engineering College 8
Generic Constraints
▪Generic Constraints are used to specify the types allowed to be
accepted in the "generic type parameter".
•where T : class
•where T : struct
•where T : ClassName
•where T : InterfaceName
•where T : new( )

Generics Rajalakshmi Engineering College 9
Generic Constraints - Example
class ClassName<T> where T : class
{
public T FieldName;
}

Generics Rajalakshmi Engineering College 10
Object of Generic Class - Example
▪ClassName<int> referenceVariable = new ClassName<int> ( ); //error

Generics Rajalakshmi Engineering College 11
Understanding Generic Constraints
▪Advantage
•You can restrict what type of data types (class names) allowed to be passed
while creating object.
▪In C#, constraints are used to restrict a generics to accept only
particular type or its derived types.
▪By using 'where' keyword, we can apply constraints on generics.
▪You can apply multiple constraints on generic classes or methods
based on your requirements.
▪Eg: where T : class where T2 : class

Generics Rajalakshmi Engineering College 12
Generic Constraint - Program
public abstract class Student
{
public abstract int Marks { get; set; }
}
public class GraduateStudent : Student
{
public override int Marks { get; set; }
}
public class PostGraduateStudent : Student
{
public override int Marks { get; set; }
}
//generic class with constraints (want to accept Student or its child classes only)
public class MarksPrinter<T> where T : Student
{
public T stu;
public void PrintMarks()
{
Student temp = (Student)stu;
System.Console.WriteLine(temp.Marks);
}
}
class Program
{
static void Main()
{
//create object of generic class
MarksPrinter<GraduateStudent> mp = new MarksPrinter<GraduateStudent>();
mp.stu = new GraduateStudent() { Marks = 80 };
mp.PrintMarks();
System.Console.ReadKey();
}
}

Generics Rajalakshmi Engineering College 13
Generic Methods
▪Generic Method is a method that has one or more generic
parameter(s).
▪You can restrict what type of data types to be allowed to be
passed to the parameter while calling the method.

Generics Rajalakshmi Engineering College 14
Generic Method – Example
public void MethodName<T>
{
}

Generics Rajalakshmi Engineering College 15
Calling Generic Method - Example
▪MethodName<datatype>( valueHere );

Generics Rajalakshmi Engineering College 16
Generic Methods - Class
public class Employee
{
public int Salary;
}
public class Student
{
public int Marks;
}
//a class with generic method
public class Sample
{
//generic method
public void PrintData<T>(T obj) where T : class
{
if (obj.GetType() == typeof(Student))
{
Student temp = obj as Student;
System.Console.WriteLine (temp.Marks);
}
else if (obj.GetType() == typeof(Employee))
{
Employee temp = obj as Employee;
System.Console.WriteLine (temp.Salary);
}
}
}

Generics Rajalakshmi Engineering College 17
Generic Methods - Program
class Program
{
static void Main()
{
//create objects
Sample sample = new Sample();
Employee emp = new Employee() { Salary = 1000 };
Student stu = new Student() { Marks = 80 };
//call the generic method
sample.PrintData<Employee>(emp);
sample.PrintData<Student>(stu);
System.Console.ReadKey();
}
}

Generics Rajalakshmi Engineering College 18
Assignment
1.What are generic classes in C#? Provide an example of how to define
and use a generic class.
2.What are generic methods in C#? Provide an example of how to define
and use a generic method.
3.What are generic constraints in C#? Why are they important?
4.Can you provide an example of using generic constraints in a real-world
project scenario?
5.How can you specify multiple generic constraints for a type parameter
in C#?
6.What is the purpose of the default keyword in generic constraints?
7.Can you explain the concept of type inference in C# with respect to
generic methods?

Queries?

Thank You…!
Tags