C # Generic Method, cannot imply conversion

I have 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 get: Cannot implicitly convert type 'Test.Cars' to 'T'

What am I missing here? All types of vehicles implement the ICAR interface.

thank

+6
source share
4 answers

You cannot convert to Tbecause T is not known at compile time. If you want your code to work, you can change the return type to ICarand remove the common return type T.

You can also use T. That will work too. If you use only the default constructor, you can also point to new()and use new T()to make your code work.

Examples

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 restriction:

public T GetCar<T>()
    where T : ICar, new()
{
    return new T();
}
+9

, , , T - BigCar - , , . ,

public static T GetCar<T>() where T : ICar, new()
{
    return new T();
}

new() ( ) .

+7

You can simplify your code

public static T GetCar<T>()
    where T : ICar, new()
{
    return new T();
}
+2
source

Generics is a run-time concept. Information about the types used in the universal data type, regardless of whether its value or reference type can be obtained at run time using reflection.

When code with T compiles to MSIL, it only identifies it as having a type parameter. Therefore, the parameter of the universal type T is not known at compile time.

class Program
{
    static void Main(string[] args)
    {
        ICar smallCar = Helper.GetCar<SmallCar>("car 1");
        ICar mediumCar = Helper.GetCar<MediumCar>("car 2");

        Console.ReadLine();
    }
}

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

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

        return (T)objCar;
    }

}

interface ICar
{
    string CarName { get; set; }
}

class SmallCar : ICar
{
    public string CarName { get; set ; }
}

class MediumCar : ICar
{
    public string CarName { get; set; }
}
0
source

All Articles