tags:

views:

460

answers:

11

In C# what does where T : class mean?

Ie.

public IList<T> DoThis<T>() where T : class
+1  A: 

'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
Randy Minder
+17  A: 

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).

Oded
@stakx - quite correct. Answer updated.
Oded
@Oded, nice. Previous comment deleted. :)
stakx
+6  A: 

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>();
Isak Savo
+8  A: 

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.

Andy Rose
@Oded - Answer updated to be more specific without looking at the linked article.
Andy Rose
You missed one case. The type argument for T can also be *any other type parameter that is constrained to be a reference type*.
Eric Lippert
+6  A: 

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 MSDN's page on the where clause, or generic parameter constraints.

Donut
+8  A: 

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

Vilx-
+2  A: 

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)

Carlos
A: 

I am not sure from which version of .net framework; I guess from .net 3.5 onwards you don't need to specify the type of <T>. You can drop the entire "where part" and now you can at compile time specify either value type or reference types for your generic type "<T>".

Pradeep
+2  A: 

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.").

AsifQadri
A: 

T refer to reference type.It may be of any type like (int ,string ,double etc).Please check this link

programmer4programming
Why did u give -1 ?
programmer4programming
int and double are not reference types. The link isn't relevant either.
Lee
Sorry.It's for Classes
programmer4programming
+2  A: 

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
 }
mumtaz