C# tutorial IT Programming Tutorials

Generics in C#

Introduction

To define generic classes and methods that can work with various types of data specified at the compile time, we use generics in C#. We use generics mainly with collections such as linked lists, hash tables, stacks, queues, and trees. Generic linked lists, stacks, queues, and trees can store any type of data such as String, Integer, Float, etc.

Uses of generics in C#

Code Readability:

Generics make the code easy to maintain and more readable and reduce the code size since there is no need for separate versions of code for different types.

Type Safety:

It provides a way to find errors at compile time rather than at run-time and avoids the need for typecasting.

Improves performance:

Generics eliminate the need for boxing and unboxing operations.

Reusability:

The generic class and method can be used with different types of data so the need for separate methods is less.

It is widely used in collections List<T>, Dictionary<TKey, TValue>, and Queue<T>.

LINQ (Language Integrated Query) to work with different types of data.

Delegates also use generics to provide type-safe functionalities.

It helps to achieve parallel programming using Task Parallel Library(TPL).

Generics In C#

Generic Method

Example 1

Explanation:

In this example, we have arrays of different types such as int and double arrays but we use the same DisplayArray<T> method to print values of both double and int data types.

Generic Class

Example 2

Explanation:

In this example, we have a generic class that includes generic parameter <T> which can work with different types of data such as integer and string objects.

Generic Interfaces:

The .Net library provides generic interfaces which are commonly used with collection classes in the System.Collections.Generic namespace.

It includes two popular interfaces such as IComparable<T> for comparing objects and IEnumerable<T>. The generic interfaces eliminate the need for boxing and unboxing operations.

Example 3:

Explanation:

The generic interface example includes an IListInterface with three methods such as add, remove, and getValues which must be implemented by the MyList class.

The MyList class provides a complete implementation for all the methods in the IListInterface.

The program. cs file includes a main method from which the actual execution begins and has a generic display method that displays all values in the list such as integer and string as given above.

Generic delegates:

Generic delegates are useful for defining delegates without creating an instance for the delegate. Some of the built-in generic delegates are Func, Action, and Predicate which are defined in System namespace.

Func: This delegate refers to a method that takes multiple input parameters(almost 16 parameters) and returns a single parameter of a specific type.

Action: It refers to a method that does not return any value but takes multiple parameters, a maximum of 16 inputs.

Predicate: It represents a method that takes a single input and returns a boolean value.

Example 4:

Explanation:

In this example, we use the built-in generic delegates such that Func refers to methods such as add, Action refers to printMsg, and Predicate refers to methods such as Notify and IsEven of String and int types.

Similar to predicate delegates other delegates such as Action and Predicate can also work with methods of different types.

Recommended Articles

Leave a Reply

Your email address will not be published. Required fields are marked *