I am writing a generic class in C# 6.0 (VS2015) that has module-level values (fields, properties, etc.) of the generic type. However, I need to include extra methods/code for certain data types (specifically int
), but I cannot figure out how assign the int's values/results to the generic-type properties in a way that the compiler will accept.
class FOO<T>
{
public T yada;
void BAR(int nom)
{
if(nom is T)
{
yada = (T)nom;
}
}
}
I cannot figure out how to get it to accept that last line: yada = (T)nom;
, it always says that it cannot convert nom
to T
.
I feel like there must be a simple way to do this and I may have even done it before, but I sure cannot remember it now. I have tried to google this extensively, but I must be using the wrong words because all it keeps returning is articles about how to type-constrain the class itself, which isn't what I'm trying to do.