C# language: Generics types

CC.png

I will say a little today about generics types, they are similar to the lists but are a bit different in that the lists are already generics types ready today, we will look into the tripe lists and see how they work from the inside.

The lists are ready classes in .NET, knowing the generics types, you can create your own class similar to lists and use it eg the queue in the lists was created like this (add a namespace at the top):

static void Main(string[] args)
{
   int variable1 = 0;
   int variable2 = 1;
   int variable3 = 2;
   int variable4 = 3;
 
   Queue<int> queue = new Queue<int>();
   queue.Enqueue(variable1);
   queue.Enqueue(variable2);
   queue.Enqueue(variable3);
   queue.Enqueue(variable4);
 
   Console.WriteLine(queue.Dequeue());
   Console.WriteLine(queue.Dequeue());
 
   Console.ReadKey();
}

From the inside the queue looks like this and the generics types are used there:

public class Queue<T>
{
   int position;
   int lastPosition = 0;
   T[] data = new T[100];
   public void Enqueue(T obj) => data[position++] = obj;
   public T Dequeue() => data[lastPosition++];
}

Now delete only the namespace without removing the first script added at the top and see the effect. In both solutions, the effect is the same, right?

How it’s working? I already explaing in the script in which we have created our own queue class, there is nothing new except this “T”.

In the “Main” function, we created the Queue object and the type that should be used in this queue, ie int, so int in the Queue class we insert in place of the “T” parameter, after creating the int type in the queue in the “Main” function, the Queue class looks like this:

public class Queue<int>
{
   int position;
   int lastPosition = 0;
   int[] data = new int[100];
   public void Enqueue(int obj) => data[position++] = obj;
   public int Dequeue() => data[lastPosition++];
}

What for are the generics types?

Generics types are used to write code with different types of data or data whose types you will not know eg if you were making a game and having a character with various statistics such as hero’s name, amount of strength, dexterity etc. and you would not be able to define types, then you could in this use generics types.

Generics methods

We will do now, for example, the generics method.

static void Main(string[] args)
{
    int a = 123;
    int b = 12;
    Swap(ref a,ref b);
    Console.ReadKey();
}
 
static void Swap<T>(ref T a,ref T b)
{
    b = a;
    a = b;
    Console.WriteLine(b);
    Console.WriteLine(a);
}

The most important thing to know in this example is that the method will generate the appropriate type based on the types of arguments passed.

Inheritance after generic classes

class Base { }
class BaseGeneric<T> { }
class GenericClass1<T> : Base { }
class GenericClass2<T> : BaseGeneric<int> { }
class GenericClass3<T> : BaseGeneric<T> { }

Implementing generics interfaces in classes

interface IBase { }
interface BaseGeneric<T> { }
class GenericClass1<T> : IBase { }
class GenericClass2<T> : IBaseGeneric<int> { }
class GenericClass3<T> : IBaseGeneric<T> { }

“default” key word

In generic types there is a default key word, it just resets the variable, the default value for int is zero.

The following example will display zero in the console.

static void Main(string[] args)
{
    int c = 435;
    ResetValue(ref c);
    Console.WriteLine(c);  
 
    Console.ReadKey();
}
static void ResetValue<T>(ref T a)
{
    a = default(T);
}

This content also you can find on my blog http://devman.pl/csharplan/c-language-generics-types/

If you recognise it as useful, share it with others so that others can also use it.

Leave upvote and follow and wait for next articles :) .

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center