175

In C# what does where T : class mean?

Ie.

public IList<T> DoThis<T>() where T : class

12 Answers 12

143

Simply put this is constraining the generic parameter to a class (or more specifically a reference type which could be a class, interface, delegate, or array type).

See this MSDN article for further details.

1
  • 4
    You missed one case. The type argument for T can also be any other type parameter that is constrained to be a reference type. Commented Sep 24, 2010 at 15:23
64

It's a type constraint on T, specifying that it must be a class.

The where clause can be used to specify other type constraints, e.g.:

where T : struct // T must be a struct
where T : new()  // T must have a default parameterless constructor
where T : IComparable // T must implement the IComparable interface

For more information, check out Microsoft's page generic parameter constraints.

2
  • 9
    It is possible to combine these together, e.g: where T : class, IComparable, new()
    – Izzy
    Commented Mar 10, 2016 at 9:49
  • must be a class? What about interface? If what class word does mean is explained, the concept can be grasped. Or, it is a defect. Commented Feb 5, 2022 at 11:24
45

It is a generic type constraint. In this case it means that the generic type T has to be a reference type (class, interface, delegate, or array type).

0
19

That restricts T to reference types. You won't be able to put value types (structs and primitive types except string) there.

1
  • 1
    This answer (and a couple of other with the same information) was more useful for me than the selected one, because it gives an example of what T cannot be (I was wondering what this constraint actually added to the story)
    – mins
    Commented Sep 20, 2019 at 10:57
11

where T: class literally means that T has to be a class. It can be any reference type. Now whenever any code calls your DoThis<T>() method it must provide a class to replace T. For example if I were to call your DoThis<T>() method then I will have to call it like following:

DoThis<MyClass>();

If your metthod is like like the following:

public IList<T> DoThis<T>() where T : class
{
   T variablename = new T();

   // other uses of T as a type

}

Then where ever T appears in your method, it will be replaced by MyClass. So the final method that the compiler calls , will look like the following:

public IList<MyClass> DoThis<MyClass>() 
{
   MyClass variablename= new MyClass();

  //other uses of MyClass as a type

  // all occurences of T will similarly be replace by MyClass
 }
2
  • 4
    -1: new T() is not possible with where T : class. you have to specify where T: new() to be allowed to do it.
    – esskar
    Commented Jan 22, 2013 at 18:02
  • @explorer can we define a single generic method and call it from multiple places to insert a record by passing different parameter's from different places.
    – Zaker
    Commented Jun 18, 2015 at 6:47
9

it means that the type used as T when the generic method is used must be a class - i.e. it cannot be a struct or built in number like int or double

// Valid:
var myStringList = DoThis<string>();
// Invalid - compile error
var myIntList = DoThis<int>();
4

It is called a type parameter constraint. Effectively it constraints what type T can be.

The type argument must be a reference type; this applies also to any class, interface, delegate, or array type.

Constraints on Type Parameters (C# Programming Guide)

4

T represents an object type of, it implies that you can give any type of. IList : if IList s=new IList; Now s.add("Always accept string.").

3

Here T refers to a Class.It can be a reference type.

1

'T' represents a generic type. It means it can accept any type of class. The following article might help:

http://www.15seconds.com/issue/031024.htm
0
public class MyGenericClass<T> where T : SomeType
{
    // Class implementation
}

Type Constraints: You can apply various constraints to type parameters, such as:

Class constraint (where T : class): This ensures that the type argument must be a reference type (a class or interface).

Struct constraint (where T : struct): This ensures that the type argument must be a value type (a struct).

Constructor constraint (where T : new()): This ensures that the type argument must have a parameterless constructor.

Interface constraint (where T : IMyInterface): This ensures that the type argument must implement a specific interface.

Base class constraint (where T : MyBaseClass): This ensures that the type argument must inherit from a specific base class.

0
where T : base-class   // Base-class constraint
where T : interface    // Interface constraint
where T : class        // Reference-type constraint
where T : class?       // (See "Nullable Reference Types" in Chapter 4)
where T : struct       // Value-type constraint (excludes Nullable types)
where T : unmanaged    // Unmanaged constraint
where T : new()        // Parameterless constructor constraint
where U : T            // Naked type constraint
where T : notnull      // Non-nullable value type, or (from C# 8)
                       // a non-nullable reference type

Excerpt From C# 12 in a Nutshell Joseph Albahari

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.