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
- <access-specifier><class keyword><class name>{
-
-
- }
Example
- public class Box {
- int height, width;
- public void SetData() {
- height = 100;
- width = 200;
- }
- public void GetData() {
- console.writeLine("height=" + height);
- console.writeLine("width=" + width);
- }
- }
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,
- Box obj=new Box();
- obj.SetData();
- obj.GetData();
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:
- public void SetData(int h, int w){
- height=h;
- width=w;
- }
and call like this,
- Box obj=new Box();
- obj.SetData(100,200);
- 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
- Box obj1=new Box();
- obj1.SetData(300,400);
- 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.
- public class Box < T > {
- public int height {
- get;
- set;
- }
- public int width {
- get;
- set;
- }
- public T Result {
- get;
- set;
- }
- public Box(int h, int w, T r) {
- height = h;
- width = w;
- Result = r;
- GetData();
- }
- public void GetData() {
- Console.WriteLine("height=" + height);
- Console.WriteLine("width=" + width);
- Console.WriteLine("Result is=" + Result + "\n");
- }
- }
- class Program {
- static void Main(string[] args) {
- new Box < string > (100, 200, "print values");
- new Box < bool > (300, 400, 400 > 300);
- new Box < long > (10, 20, 10 * 2020);
- new Box < long > (10, 20, 20 + 30);
- Console.ReadKey();
- }
- }
- 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).
- 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.
- 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.
- 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.
- public class Repository < T > : where T: class {
- public List < T > GetAll() {
- return ....ToList();
- }
- }
- Repository < Student > repoStudent = new Repository < Student > ();
- 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.