DEV Community

fadyehabamer
fadyehabamer

Posted on

Generics In Csharp

blog image

In C#, you can write code that works with a variety of data types by using generics.
As a result, the code is more flexible and reusable. Generic declarations are made with the <> sign.
For instance, the code that follows defines the generic method Print:

public static void Print<T>(T value)
{
    Console.WriteLine(value);
}
Enter fullscreen mode Exit fullscreen mode

Any data can be printed using this technique. The code that outputs a string **and an **integer, for instance, is as follows:

Print(10); // 10
Print("Hello, world!"); // Hello, world!
Enter fullscreen mode Exit fullscreen mode

Generics can also be used to create generic classes.
For example, the following code declares a generic class called List:

public class List<T>
{
    private T[] items;

    public List()
    {
        items = new T[0];
    }

    public void Add(T item)
    {
        items = Array.Resize(items, items.Length + 1);
        items[items.Length - 1] = item;
    }

    public T Get(int index)
    {
        return items[index];
    }
}
Enter fullscreen mode Exit fullscreen mode

Any kind of data can be stored in this class. For instance, the code that follows builds an integer list and appends some numbers to it:

List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
Enter fullscreen mode Exit fullscreen mode

Generics are an effective technique for increasing the versatility and reusability of code. They are an essential component of the C# language and come in a variety of uses.

The following are some advantages of utilizing generics in C#:

  1. They increase the versatility of coding. Because generic code works with a variety of data types, it is more reusable.

  2. They improve the conciseness of the code. Compared to non-generic code, generic code can frequently be expressed more succinctly.

  3. They could enhance output. There are situations when generic code can increase performance by removing the requirement for type casts.

  4. They could aid in error prevention. Because generic code makes sure the right data types are utilized, it can help prevent problems.

Generic Classes

The symbol should be used after the class name to declare the Generic class. It is not required to include the word "T" in the Generic type definition. Any word in the TestClass<> class declaration is acceptable.

public class TestClass<T> { }
Enter fullscreen mode Exit fullscreen mode

The System.Collection.Generic namespace also defines a number of classes that implement many of these key interfaces.
The following table describes the core class types of this namespace.

Generic class Description
Collection The basis for a generic collection Comparer compares two generic objects for equality
Dictionary A generic collection of name/value pairs
List A dynamically resizable list of Items
Queue A generic implementation of a first-in, first-out (FIFO) list
Stack A generic implementation of a last-in, first-out (LIFO) list

In conclusion,
generics are an essential feature of the C# language, offering developers a powerful mechanism for crafting versatile and reusable code. Whether in methods or classes, leveraging generics can significantly enhance the flexibility and efficiency of your C# applications.

Top comments (0)