C# Generics: Constraining T where T : Object doesn't compile; Error: Constraint cannot be special class 'object'

.net, c#, clr, generics, object

Solution

There is no difference between the two constraints, except for that one is disallowed for being useless to explicitly state.

The C# 4.0 language specification (10.1.5 Type parameter constraints) says two things about this:

The type must not be object. Because all types derive from object, such a constraint would have no effect if it were permitted.

...

If T has no primary constraints or type parameter constraints, its effective base class is object.

In your comment, you said that you were trying to make `T` be of type `Void`. `Void` is a special type that indicates that there is no return type and cannot be used in place of `T`, which requires an appropriate concrete type. You will have to create a void version of your method and a `T` version if you want both.

Problem

When I constrain T with : Object like this: ``` public interface IDoWork<T> where T : Object { T DoWork(); } ``` I get the error: Constraint cannot be special class 'object' Does that mean there is an implied difference with the following that does compile? ``` public interface IDoWork<T> // where T : Object { T DoWork(); } ```

Original source

Related problems