Explain Generics in C#? When and why to use them in real applications? with code

Generics allows us to make classes and methods - type independent or type safe.

The problem is, it involves Boxing from converting string (value) to object (reference) type. This will impact the performance.

Generics in C# provide a way to create reusable and type-safe code by allowing the definition of classes, interfaces, and methods that can work with various data types. Generics enable the creation of components that can operate on different types without sacrificing type safety or requiring code duplication.

Generics are used when you want to create code that is independent of a specific data type and can be reused with different types. They offer the following benefits:

Reusability: With generics, you can create classes, interfaces, and methods that can be used with multiple data types. This promotes code reuse, reduces duplication, and improves maintainability.

Type Safety: Generics ensure compile-time type safety by allowing you to specify the type(s) to be used with the generic component. The compiler checks the type compatibility, preventing type-related errors at runtime.

Performance: Generics provide better performance compared to non-generic approaches such as using object or boxing/unboxing. Generics avoid unnecessary casting or boxing operations and allow for more efficient code execution.

Here's an example that demonstrates the usage of generics in a simple class:

public class Stack<T> { private List<T> items = new List<T>(); public void Push(T item) { items.Add(item); } public T Pop() { if (items.Count == 0) throw new InvalidOperationException("Stack is empty."); T item = items[items.Count - 1]; items.RemoveAt(items.Count - 1); return item; } public bool IsEmpty() { return items.Count == 0; } }

In the above example:

  • The Stack<T> class is a generic class that can work with any type T.
  • The Push method accepts an item of type T and adds it to the internal list.
  • The Pop method removes and returns the top item from the stack, of type T.
  • The IsEmpty method checks if the stack is empty.

With the generic Stack<T> class, you can create stacks of various types:

Stack<int> intStack = new Stack<int>(); intStack.Push(1); intStack.Push(2); intStack.Push(3); Stack<string> stringStack = new Stack<string>(); stringStack.Push("Hello"); stringStack.Push("World"); Console.WriteLine(intStack.Pop()); // Output: 3 Console.WriteLine(stringStack.Pop()); // Output: World

In this example, both intStack and stringStack are instances of the Stack<T> class, but one is used with int type and the other with string type.

Generics are used in real applications when you want to create reusable code components that can work with different data types. They are commonly used in collections, algorithms, data structures, and many other scenarios where type independence and code reuse are important. Generics help in writing cleaner, more efficient, and type-safe code, enabling better maintainability and flexibility.

 

Post a Comment

Previous Post Next Post