I\'ve got the following code:
public static T GetCar() where T : ICar
{
T objCar = default(T);
if (typeof(T) == typeof(SmallCar)) {
Your code is illegal because while you might be testing and know that your given T is BigCar or some other such type, the compiler cannot know that in advance and therefore the code is illegal. Based upon your given usage, you could have
public static T GetCar() where T : ICar, new()
{
return new T();
}
The new() constraint allows you to invoke the default (parameterless) constructor on a type.