Make it a static field.
public class TestClass<T> { static T TrueIfBoolean = typeof(T) == typeof(bool) ? (T)(object)true : default(T) public T TestMethod() { return TrueIfBoolean; } }
Thus, boxing / unpacking occurs only once. It is also quite possible that such optimization is performed in any case, even in the source code.
source share