Nel*_*mel 6 c# generics boxing unboxing
下面是一些快速代码来说明我的问题.有什么方法可以避免这种明显不必要的拳击/拆箱?
public class TestClass<T>
{
public T TestMethod()
{
if (typeof(T) == typeof(bool))
{
return true; // doesn't work
return (T)(object)true; // works, but any way to avoid this?
}
return default(T);
}
}
Run Code Online (Sandbox Code Playgroud)