Generic Classes And Functions

Introduction

 
This article introduces working with generic classes and functions. This article will help you create some T type classes for the application that help manage data redundancy and maintain code neatly.
 
Let's start first by creating a Box class. Then we will create T type classes and make its functions T type also.
 

Class

 
Class is a user-defined data type. It is a collection of data members and functions. It is used to categorize our program.
 
Syntax
  1. <access-specifier><class keyword><class name>{  
  2.    //data members - variables, constants, and properties  
  3.    //data functions  
  4. }   
Example
  1. public class Box {  
  2.     int height, width;  
  3.     public void SetData() {  
  4.         height = 100;  
  5.         width = 200;  
  6.     }  
  7.     public void GetData() {  
  8.         console.writeLine("height=" + height);  
  9.         console.writeLine("width=" + width);  
  10.     }  
  11. }  
In the above example, there is a Box class that contains two variables, height, width. In the SetData() function it sets the value of height & width variables. In the GetData() function it prints the value of height & width. So, if we want to call the Box class, then we have to call like this,
  1. Box obj=new Box();  
  2. obj.SetData();//set the values of height&width;  
  3. obj.GetData();//print the values  
In this overall process, we saw one thing that we have to fix the values of height & width inside the class. We can pass it dynamically like this:
  1. public void SetData(int h, int w){  
  2.    height=h;  
  3.    width=w;  
  4. }  
 and call like this,
  1. Box obj=new Box();  
  2. obj.SetData(100,200);//set the values of height&width;  
  3. obj.GetData();  
But still, we saw that data of this class was permanently set, for a class only. And If we want to pass different heights & widths then, we have to create an additional object of this class.
 
In this way
  1. Box obj1=new Box();  
  2. obj1.SetData(300,400);//set the values of height&width;  
  3. obj1.GetData()  
For a programmer, it's not a good practice to call the same code repeatedly. So the concept of generic classes comes under the C# technology.
 
In the generic classes, we can make T type of data and properties.
 
Here, we create a class of T type with the T type of data. In the constructor, we assign the dynamic values to local variables and call a GetData() function to print the values.
  1. public class Box < T > {  
  2.     public int height {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public int width {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public T Result {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public Box(int h, int w, T r) {  
  15.         height = h;  
  16.         width = w;  
  17.         Result = r;  
  18.         GetData();  
  19.     }  
  20.     public void GetData() {  
  21.         Console.WriteLine("height=" + height);  
  22.         Console.WriteLine("width=" + width);  
  23.         Console.WriteLine("Result is=" + Result + "\n");  
  24.     }  
  25. }  
  26. class Program {  
  27.     static void Main(string[] args) {  
  28.         new Box < string > (100, 200, "print values");  
  29.         new Box < bool > (300, 400, 400 > 300);  
  30.         new Box < long > (10, 20, 10 * 2020);  
  31.         new Box < long > (10, 20, 20 + 30);  
  32.         Console.ReadKey();  
  33.     }  
  34. }  
  1. new Box<string>(100, 200, "print values");  
It calls a Box constructor, provides us string type results as we pass the third parameter of T type (any type).
  1. new Box<bool>(300, 400, 400 > 300);  
It calls a Box constructor, provides us bool type result as we pass the third parameter of Comparison type.
  1. new Box<long>(10, 20, 10 * 2020);  
It calls a Box constructor, provides us a long type result as we pass the third parameter of multiplication of two values.
  1. new Box<long>(10, 20, 20+30);  
It calls a Box constructor, which provides us a long type result as we pass the third parameter of the addition of two values.
 
Here we saw that we generate different types of data bypassing distinct types of values. But we create the class-only object type if we wish to make a class of class type as we needed in the Repository classes for passing database classes then, we can create the T type classes by defining the type of T before it.
  1. public class Repository < T > : where T: class {  
  2.     public List < T > GetAll() {  
  3.         return ....ToList();  
  4.     }  
  5. }  
  6. Repository < Student > repoStudent = new Repository < Student > ();  
  7. repoStudent.GetAll();  
Here, we indicate the T is the class type only means now it not became the object type or not accept the object type data. Function GetAll() also is the List<T> type means now it returns any type or class listing. 
 
Output
 
 

Summary

 
In this article, we have learned concepts of generic classes and functions and learn to deal with them for better programming.