我一個枚舉,延伸短:爲什麼不能使用這種通用方法調用?
public enum EnumType : short
{
Value1 = 1,
Value2 = 2
}
然後,我有一個看起來像這樣的實用方法:
public static class EnumUtilities
{
public static IEnumerable<Tuple<TEnum, TBase>> GetTuples<TEnum, TBase>()
where TEnum : struct, TBase
{
return GetValues<TEnum>().Select(x => new Tuple<TEnum, TBase>(x, x));
}
public static IEnumerable<T> GetValues<T>() where T : struct
{
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
當我嘗試調用它,我得到一個編譯錯誤說的」類型EnumType必須可以轉換爲'short'才能在通用方法'IEnumerable> GetTuples()'中使用它作爲參數'TEnum''
我無法理解EnumType如何不能轉換爲'當它被定義爲分機時,「簡短」結束'短'。我還沒有理解如何以下編譯,但上面的例子沒有。任何幫助表示讚賞!
var enumType = EnumType.Value1;
var value1 = (short)enumType;
'Enum'默認的類型是'int'的。所以隱式轉換爲「short」是不可能的。 – AgentFire 2014-10-02 18:21:23