C# Generics

The programming language C# includes a crucial functionality called generics, allowing the creation of classes, methods, structures, delegates, and interfaces with a placeholder for types. These placeholders are substituted by the compiler with the designated type at compile time, ensuring type safety and enhancing performance simultaneously.

The <style> element includes a CSS styling block with a background color gradient, border radius, padding, margin, and centered text alignment. Within this block, there is a CSS class for the placeholder icon with specified font size and margin, as well as a CSS class for the placeholder text with a defined color and font size. </style>

Generics are primarily utilized for developing versatile, reusable classes and functions that can operate with various data types without redundancy in code. To specify a generic class, angle brackets (<>) must be employed. These brackets are essential for designating a class or function as a generic type and accommodating multiple type parameters.

Syntax:

It has the following syntax.

Example

public returnType MethodName<T>(T parameter)

{

    // Method body

}

In this syntax,

  • T: It represents a generic type parameter.
  • returnType: It specifies the method's return type.
  • MethodName: It represents the name of the method.
  • C# Generic Example

Let's consider an example to demonstrate the concept of generics in C#.

Example

Example

using System;

class C# Tutorial

{

    // Generic method

    public static void Display<T>(T value)

    {

        Console.WriteLine("The value is " + value);

    }

    static void Main()

    {

        // Calling Generic method

        Display<int>(100);          

        Display<string>("John");   

        Display<double>(3.14);      

        Display<bool>(true);        

    }

}

Output:

Output

The value is 100

The value is John

The value is 3.14

The value is True

Explanation:

In this instance, we are showcasing the generic function named Display, capable of receiving various data types like integers, strings, doubles, or booleans. Upon invoking the Display function, the compiler dynamically substitutes T with the specific data type, and then outputs the result.

Types of Generics in C#

Generic facilitates the development of adaptable, recyclable, and strongly-typed code. There exist multiple approaches to employing generics in C#. A few of these methods include:

The <style> element is crucial for defining the styling of placeholders on a webpage. It includes specifications for the background color, border radius, padding, margin, and text alignment. The .placeholder-icon class within this element determines the size and spacing of the icon used for placeholders, while the .placeholder-text class controls the color and size of the placeholder text. These CSS rules ensure placeholders are displayed consistently and aesthetically on the website.

Here, we will discuss these generics one by one.

1) Generic Class

A generic class is a crucial element in C# as it enables the creation of type-safe data structures or classes that can operate on various data types. The specific data type is determined during the instantiation of the class, enhancing flexibility and promoting the reuse of code.

C# Generic Class Example

Let's consider an instance to demonstrate the generic class in C#.

Example

Example

using System;

// Generic class

class Box<T>

{

    public T item;

}

class Program

{

    static void Main(string[] args)

    {

        Box<int> b = new Box<int>();

        b.item = 5;

        Console.WriteLine("Integer: " + b.item);

        Box<string> bb = new Box<string>();

        bb.item = "Hello";

        Console.WriteLine("String: " + bb.item);

    }

}

Output:

Output

Integer: 5

String: Hello

Explanation:

In this instance, a class named Box<T> has been instantiated, where T represents any data type. Within the primary function, two objects of this generic class are instantiated - one for integers and another for strings. The initial object, denoted as b, stores an integer value, while the subsequent object, bb, stores a string value.

2) Generic Method

In C#, a generic function is a function that is declared with numerous type parameters, allowing it to work with various data types in a manner that ensures type safety and avoids duplicating code. This feature is beneficial when there is a need to create a function capable of processing diverse types of data without compromising on performance or type safety.

Example of a Generic Method in C#

Let's consider a scenario to demonstrate the universal approach in C#.

Example

Example

using System;

public class C# Tutorial

{

    // Generic Method

    public static void Swap<T>(ref T a, ref T b)

    {

        T tmp = a;

        a = b;

        b = tmp;

    }

    public static void Main()

    {

        int m = 5, n = 10;

        Console.WriteLine("The values before swapping are: m = " + m + ", n = " + n);

        Swap(ref m, ref n);

        Console.WriteLine("After swapping: m = " + m + ", n = " + n);

        string f = "Hello World", s = "Tech";

        Console.WriteLine("\nBefore swapping: first = " + f + ", second = " + s);                        Swap(ref f, ref s);

        Console.WriteLine("After swapping: first = " + f + ", second = " + s);

    }

}

Output:

Output

The values before swapping are: m = 5, n = 10

After swapping: m = 10, n = 5

Before swapping: first = C# Tutorial, second = Tech

After swapping: first = Tech, second = C# Tutorial

Explanation:

In this illustration, we are examining a class named C# Tutorial that features the exchange<T> function. This function accepts two parameters by reference with the ref keyword. The function is designed to interchange the values by utilizing a temporary variable. Within the main function, the script initially exchanges two integers, m and n, displaying their values both pre and post swapping. Following this, it proceeds to swap two strings, f and s, and displays the results.

3) Generic Interface

In C#, a generic interface allows us to establish an agreement with several type parameters, enhancing its adaptability to handle different data types. By utilizing a type parameter, redundant code for various data types is eliminated, promoting both type safety and code reusability.

C# Generic Interface Example

Let's consider an instance to demonstrate a generic interface in C#.

Example

Example

using System;

// Generic interface

public interface ICalc<m>

{

    void square_root(m value);

}

public class dCalc : ICalc<double>

{

    public void square_root(double v)

    {

        double result = v * v;

   Console.WriteLine("The square of a number is " + result);

    }

}

public class C# Tutorial

{

    public static void Main()

    {

        ICalc<double> c = new dCalc();

        c.square_root(10); 

    }

}

Output:

Output

The square of a number is 100

Explanation:

In this instance, we are illustrating an interface named ICalc<m> which specifies a function called squareroot that accepts a parameter of a generic type m. Following this, a class named dCalc is established to implement the interface utilizing the double data type. Within the squareroot function, the computation for the square root of the given number is performed. Subsequently, within the main function, an object is instantiated using the ICalc<double> interface, and the square_root function is invoked to display the result.

4) Generic Delegates

In C#, a generic delegate is a form of delegate that is specified with several type parameters, allowing it to operate with various data types. By utilizing a generic delegate, there is no need to generate separate delegates for distinct data types, as it can be defined once and employed with any type parameter as needed.

C# Generic Delegate Example

Let's consider an example to demonstrate the generic delegates in C#.

Example

Example

using System;

namespace C# Tutorial

{		

    // Generic delegate	

    public delegate T Op<T>(T x, T y);

    class tech

    {

        // Method 

        static int add( int p, int q )

        {

            return p + q;

        }

        static string addchar( string p, string q )

        {

            return p + q;

        }

        static void Main(string[] args)

        {

            Op<int> o = add;

            Console.WriteLine("The Addition of integers: " + o(10, 20) );

            Op<string> Os = addchar;

            Console.WriteLine("The Concatenation of strings: " + Os("Hello, ", "World!") );

        }

    }

}

Output:

Output

The Addition of integers: 30

The Concatenation of strings: Hello, World!

Explanation:

In this illustration, a general delegate named Op<T> is showcased, designed to operate with various parameters. Subsequently, functions are established to sum integers and merge strings. Within the primary method, the delegate is employed to sum numbers with an integer and combine text with a string.

5) Generic Collection

In C#, a generic collection is a special type of collection that is used to store any data type safely. These collections are defined in the System.Collections.Generic namespace. It is generally used in several collections, such as List<T>, Dictionary<TKey, TValue>, Queue<T>, Stack<T>, and many others.

C# Generic Collection Example

Let's consider an example to demonstrate the generic collection in C#.

Example

Example

using System;	

using System.Collections.Generic;

class C# Tutorial

{

    static void Main()

    {

        List<int> x = new List<int>();

        x.Add(8);

        x.Add(16);

        x.Add(32);

        Dictionary<string, int> a = new Dictionary<string, int>();

        a.Add("Michael", 45);

        a.Add("Bravo", 50);

        a.Add("Johnson", 35);

        Console.WriteLine("The numbers are: ");

        foreach (int nn in x)

        {

            Console.WriteLine(nn);

        }

        Console.WriteLine("The ages are: ");

        foreach (var item in a)

        {

            Console.WriteLine(item.Key + ": " + item.Value);

        }

    }

}

Output:

Output

The numbers are: 

8

16

32

The ages are: 

Michael: 45

Bravo: 50

Johnson: 35

Explanation:

In this instance, a List<int> is employed to hold integer values, while a Dictionary <string, int> is utilized to store names along with their corresponding ages. Subsequently, a foreach loop is implemented to display their values in a manner that ensures type safety.

Advantages of using Generics in C#

There are numerous benefits of generics in C#. Here are some of the key advantages:

Here, we will explore these benefits individually.

1) Code Reusability

In C#, the concept of generics enhances code reusability by allowing the creation of a unified method, class, or interface that can operate with various data types.

2) Type Safety

Generics enhance type safety by enforcing the use of a particular data type, leading to the detection of errors during compilation rather than execution. This results in increased reliability and resilience of the code.

3) Performance

Generic type offers superior performance in contrast to non-generic types by minimizing various redundant operations like boxing, unboxing, and explicit type casting.

4) Support for Multiple Type Parameters

In C#, Generic classes and methods have the capability to receive multiple types of parameters. This feature provides increased flexibility, making the code structure more adaptable and reusable.

5) Collections

In C#, generics are applicable in various collections like List<T>, Dictionary<T, U>, among others. They offer type-safe and effective data structures.

Conclusion

In summary, C# generics play a crucial role by allowing us to specify classes and functions with a placeholder for data types. These placeholders are substituted with the actual type during the compilation process. This functionality empowers us to create versatile methods and classes that can work with various data types. By leveraging generic classes, methods, interfaces, delegates, and collections, we can develop applications that exhibit enhanced efficiency, adaptability, and manageability.

C# Generics FAQs

1) What is Generic in C#?

In C#, generics enable us to specify classes and methods with a placeholder for types. During compilation, the compiler substitutes these type placeholders with the designated type, promoting the development of applications with enhanced flexibility, type safety, and code reusability.

Yes, in C# Generics, it is possible to utilize multiple type parameters.

Yes, it is possible to utilize multiple type parameters in Generics in C#.

Example

public class Pair<T1, T2>

{

    public T1 First;

    public T2 Second;

}

3) What are the default (T) generics in C#?

In C#, the ```

public returnType MethodName<T>(T parameter)

{

// Method body

}

Example


4) Define a Generic collection in C#?

In C#, a generic collection is a special type of collection that can store any data type safely. It is defined in the System.Collections.Generic namespace. It is generally used in collections such as List<T>, Dictionary<TKey, TValue>, Queue<T>, Stack<T>, and many others.

5) What is the downside of Generics in C#?

When generics are improperly implemented in C#, it can result in heightened code intricacy and potential misuse. This situation may result in decreased code readability, particularly for novice programmers, and working with reflection alongside generics can become more intricate. Conversely, employing generics correctly can help mitigate these challenges.

Input Required

This code uses input(). Please provide values below: