Generics types

Generics allow us to design classes and methods without the notion of data types. In simpler terms, when we talk about methods, generics allow us to define methods without specifying the type of the input variables. 

Let's go through the following code implementation and see how it can help us. In the following example, we have created a function that compares the values between two int variables, A and B. If the value is the same, it returns true; however, if the value is not same, it returns false:

static private bool IsEqual(int A, int B)
{
if(A== B)
{
return true;
}
else
{
return false;
}
}

Now, let's say we try to pass a variable with a data type that is not int. In the following screenshot, we are trying to pass string instead of int, to which the compiler gives us an error:

As illustrated in the following screenshot, it will give us the following error:

As illustrated by the preceding screenshot, the IsEqual function is accepting inputs of the int type. However, while calling the function, we are passing variables of the string type. Due to the type mismatch, the compiler is showing the error.

To correct this error, we need to make the IsEqual function generic. We can do this by altering the function so that instead of accepting the input variables of the int type, it can accept the input variables of the object type.

Please note that all of the variables in C# inherit from object

In this code example, we are calling the IsEqual function twice and are passing different input parameters. In the first call, we are passing string; however, in the second call, we are passing int. Note that when we compile the project, no compile time error is retrieved and the function compares the passed variables irrespective of type:

static void Main(string[] args)
{
UnSafeExample();
IsEqual("string", "string");
IsEqual(10, 10);
}

static private bool IsEqual(object A, object B)
{
if (A == B)
{
return true;
}
else
{
return false;
}
}

Although the preceding code implementation will be generic for all of the data types, it will lead to the following issues:

  • Performance degradation: In the IsEqual function definition, the data types of variables is object. Due to this, for all calls being made to this function, the variables will need to be converted from their original type, that is, int or string, into object. This conversion will be an extra load for the application, which will lead to performance degradation. In programming terms, this conversion is known as boxing and unboxing, which we will cover shortly in this chapter. 
  • Type unsafe: This approach will not be type unsafe. For example, I will call the function by passing the following variables:
IsEqual(10, "string");

If I do so, the compiler will not give any error, even though we understand that the call makes no sense. To avoid these issues while still providing us with the capability of making the calls generic, C# provides us with the tool of using generic types.

Using generic types, we can avoid specifying any data type to the input variables of the functions. Hence, the implementation of IsEqual will look like this:

static private bool IsEqual<T>(T A, T B)
{
if (A.Equals(B))
{
return true;
}
else
{
return false;
}
}

In the preceding code example, please note that we are using T to illustrate the data type, hence making it generic for all data types. 

As we are not using object, there will be no boxing and unboxing of variables. If we still try to pass incorrect data types to this function, as illustrated in the following screenshot, the compiler will give us an error:

In the next topic, we will not go through the different concepts C# uses to work on the types of the data variables. We will go through how we can use boxing and unboxing in C# to convert one data type into another and the different things we should keep in mind when we are consuming variables of different types.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.175