Below is some quick code to illustrate my question. Any way to avoid this apparently unnecessary 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);
}
}
Cast<TResult>(this IEnumerable source)
, but with a few custom cases such as "Y"/"N" needing manual conversion to true/false. If bool were my own type I could use an explicit operator. All the other types are handled generically. – SamaTestClass<bool>
version of the generic. – SamaFunc
is the way. See stackoverflow.com/questions/45507393 – Koumis