Working with Generics in C#

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

C# Programming Guide

One of the most striking C# programming language features is known as generics. Introduced in C# 2.0, it helps eliminate redundant code, enforce type safety, and promote code reusability and maintainability. It is most commonly known for its use in collections and constraints on generic types. You can use generics to define a class, method, or interface that can work with any data type.

If you are a developer using generics, you need not specify the type until the methods or classes are accessed. This promotes type safety, code maintenance, code efficiency, and also improves performance. .NET programmers can use generics to define their generic interfaces, classes, methods, events, and delegate functions as well.

This C# programming tutorial takes a deep dive into the core concepts of generics, how they work, and how coders can program generics in C#.

Read: C# Tools for Code Quality

What are Generics in C#?

The C# programming language is strongly typed. This means that, in C#, a type should be defined before data is stored. Although type safety helps ensure safety and interoperability across languages targeting the controlled environment, as a developer, you are limited to choosing the type of each object you need to work with type-safe data without the need for boxing and unboxing.

Generics are a feature in the C# programming language that enables defining classes and methods that take a type as a parameter. It lets developers create classes and methods whose types are specified at declaration time but determined when you create instances of such classes or call those methods at runtime. Programmers can easily create universal classes that help improve flexibility, performance, maintainability, and type safety.

Why Should Programmers Use Generics?

The support for type parameters in generics enables C# coders to create classes and methods that defer the definition of one or more types until the class or method is defined and instantiated by the client code. Generics enhance type safety, performance, and code reduction. It is also known as parametric polymorphism, since it encourages the use of parameterized types on our types.

Generics allow developers to create reusable code that is type-safe and efficient. With generics, you can develop algorithms capable of operating on a variety of data types. You can, for instance, create an algorithm to sort an array of integers, doubles, or even strings. Such sorting algorithms, without generics, would often require several sort methods – one for each type.

Generics help to construct type-safe collections, as well as classes and functions that can take a type as a parameter. Once defined, such classes can operate with any type. This greatly increases the manageability, extensibility, maintainability, and efficiency of your code.

Usage of generics helps avoid boxing and unboxing overheads and eliminates the need to typecast objects to their respective types. It should be noted that the .NET or .NET Core compiler verifies generic types at compile time to ensure that your code is type-safe.

Read: Improving String Performance in .NET

How Do Generics Work in C#?

The Common Language Runtime (CLR) converts generic types to intermediate language (IL) and metadata in the same way that it does for other types, but it also saves additional information about the generic types that is used to tie the generic type to a particular type at runtime when the generic type is instantiated.

When generic types are bound to value types, the generic types are instantiated for each value type it is bound to. On the contrary, generics work differently for reference types. For example, if you have a generic type bound to a reference type, then an instance of the generic type will refer to the memory address of the reference type to which it is bound for all objects of the reference type.

How Do C# Generics and C++ Templates Compare?

C# generics and C++ templates are quite similar in syntax. Generics in C# – and templates in C++ – are language features that permit parameterized types, but there are subtle differences between the two. C# generic types are strongly typed and instantiated at runtime, while C++ templates are weakly-typed and created only during compilation. Unlike C++ templates, generics do not permit default values for type arguments.

C++ templates use a compile-time model. A template is like a complex macro processor when used in a C++ program. C# generics are both a compiler and a runtime feature. Generic types, such as List, retain their generic-ness after being constructed. In other words, the substitution that the C++ compiler performs at compile time is performed by the JIT compiler.

How to Program Generics in C#

We have had enough discussion on the concept of generics – let’s now get started programming generics in C#. To create a class that works on a generic type, programmers should specify the generic type parameter when creating the class as shown in the code snippet given below:

public class MyGenericsExample 
{
    public void DisplayText() 
      {
          //Some code
      }
  //Other members
}

To define a generic list class, you include a type parameter section in the class declaration. The following example shows a simple generic class in C#:

public class GenericList
{
private class Node
{
// Internal data members.
private Node _next = null;
private T _data;
public Node(T t) { _data = t; }
public Node Next { get { return _next; } set { _next = value; } }
public T Data { get { return _data; } set { _data = value; } 
}
}

Read: Productivity Tools for .NET Developers

What are Generic Methods in C#?

A generic method is a type parameterized method that can be called with different types. In other words, it allows you to use the same function for several data types. For example, you can create one generic method to sort all arrays of ints and strings. Generic methods work in a very similar way that classes do, but they contain the type parameter list after the method name.

Generic methods are called by using their method name and type parameters, just like with class names. But if there’s no ambiguity about which generic method is actually being called, you do not need to provide them.

A generic method can be used with any data type, so it is important to specify the types in your code when calling a generic method. A generic method is used to specify the type parameter with angle brackets <>. It should be noted that you can even call a generic method by specifying its type arguments or you can omit its type arguments. In both cases, the compiler will infer the required types from the method’s arguments.

The following code snippet illustrates how you can define a generic method in C#:

public T MaxMethod(T a, T b)
{
if (a.CompareTo(b) < 0)
return b;
else
return a;
}

Final Thoughts on Generics in C# and .NET

Generic is an in-built feature in C# that enables you to build applications with cleaner, maintainable code. In this C# programming tutorial, we have had a look at the core concepts pertaining to generics and a few code examples to illustrate the concepts.

Read more C# programming and software development tutorials.

Joydip Kanjilal
Joydip Kanjilal
A Microsoft Most Valuable Professional in ASP.NET, Speaker, and Author of several books and articles. More than 25 years of experience in IT with more than 18 years in Microsoft .NET and its related technologies. He was selected as a Community Credit Winner at http://www.community-credit.com several times. He has authored 8 books and more than 500 articles in some of the most reputed sites worldwide including MSDN, Info World, CodeMag, Tech Beacon, Tech Target, Developer, CodeGuru, and more.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read