Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generic Method, cannot implicit convert

I've got the following code:

public static T GetCar<T>() where T : ICar
{
    T objCar = default(T);

    if (typeof(T) == typeof(SmallCar)) {
        objCar = new SmallCar("");
    } else if (typeof(T) == typeof(MediumCar)) {
        objCar = new MediumCar("");
    } else if (typeof(T) == typeof(BigCar)) {
        objCar = new BigCar("");
    }

    return objCar;
}

And this is the error I am getting: Cannot implicitly convert type 'Test.Cars' to 'T'

What Am I missing here? All car types implement the ICar interface.

Thanks

like image 400
MeTitus Avatar asked Apr 07 '12 19:04

MeTitus


2 Answers

You cannot convert to T because of the fact that T isn't known at compile time. If you want to get your code to work you can change the return type to ICar and remove the generic T return type.

You also can cast to T. This would work too. If you only using the default constructor you can also constain on new() and use new T() to get your code to work.

Samples

public ICar GetCar<T>()
    where T : ICar
{
    ICar objCar = null;

    if (typeof(T) == typeof(SmallCar)) {
        objCar = new SmallCar();
    } else if (typeof(T) == typeof(MediumCar)) {
        objCar = new MediumCar();
    } else if (typeof(T) == typeof(BigCar)) {
        objCar = new BigCar();
    }

    return objCar;
}

Cast:

public T GetCar<T>()
    where T : ICar
{
    Object objCar = null;

    if (typeof(T) == typeof(SmallCar)) {
        objCar = new SmallCar();
    } else if (typeof(T) == typeof(MediumCar)) {
        objCar = new MediumCar();
    } else if (typeof(T) == typeof(BigCar)) {
        objCar = new BigCar();
    }

    return (T)objCar;
}

New-constraint:

public T GetCar<T>()
    where T : ICar, new()
{
    return new T();
}
like image 174
Felix K. Avatar answered Oct 22 '22 00:10

Felix K.


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<T>() where T : ICar, new()
{
    return new T();
}

The new() constraint allows you to invoke the default (parameterless) constructor on a type.

like image 27
Anthony Pegram Avatar answered Oct 22 '22 00:10

Anthony Pegram