Learn Generics in C#

Leslie Ramírez
3 min readOct 18, 2020

--

As you may know C# is a strongly typed programming language, that implies once one variable, class or method is declared as a certain type, It will always behave as that type(unless in reassigned).

string unicorn = “8”;

The unicorn variable always be a string type and never is going to be an int for example. In certain weakly type language, you could do something like this:

unicorn = “8”

horse = unicorn + 8

//horse now is 16

This could sound nice, but it could make it harder to understand your code as the application grows in size, it becomes too much to keep track of in your head. The ups and downs in strongly typed language is that you are forced to make the behavior of your program explicit. But what happens when you need to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated?

That’s when Generics come to save the day.

So How it works?

Generics allows you to define the specification of the data type of programming elements in a class or a method, until it is actually used in the program. The type parameters in C# Generics allow you to create type-safe code without knowing the final type you will be working with. That implies you can create a class or method that can work with any data type.

The class or generic methods combine code reuse, type safety, and performance. Generics are most frequently used with collections and the methods that operate on them. It also helps to avoid unpleasant problems like type casting and boxing.

A generic type is declared by specifying a type parameter in angle brackets after a type name where Typename<T> where T is a type parameter. It isn’t mandatory to put the “T” word in the Generic type definition. You can use any word in the TestClass<> class declaration.

public class TestClass<T> { }

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.

Features of Generics

Generics is a technique that enriches your programs in the following ways:

  • It helps you to maximize code reuse, type safety, and performance.
  • You can create generic collection classes. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. You may use these generic collection classes instead of the collection classes in the System.Collections namespace.
  • You can create your own generic interfaces, classes, methods, events, and delegates.

There are some guidelines or good practice that you should take in count when working with generics, like type parameter naming guidelines:

Generics Methods

As I mentioned before we are able to create generics methods with a type parameter. The following code shows this concept:

Summarize:

  • Use generic types to maximize code reuse, type safety, and performance.
  • The most common use of generics is to create collection classes.
  • The .NET class library contains several generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible instead of classes such as ArrayList in the System.Collections namespace.
  • You can create your own generic interfaces, classes, methods, events, and delegates

References:

--

--

Leslie Ramírez

I am Microsoft MVP in the award category: Developers technologies, a professional with several years in software development specifically in .NET technology.