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 回答1: You cannot