I’m trying to implement a class that stores a generic nullable type:
public class myclass<T?>
{
T? myvariable;
public T? myfunction(){
return myvariable;
}
}
And while the above class compiles just fine, the actual use that provides trouble:
myclass<int?> c = new myclass<int>();
int? = c.myfunction(); // does not work: implicit cast from type T? in int? not possible.
// or, assuming the variable is not null
int = c.myfunction().Value; // implicit cast from type T in int not possible.
What am I doing wrong or how can I work around this?
Neither of the examples compile:
<T?>isn’t valid syntax; it should be<T>myclassneeds awhere T : structconstraintmyclass<int> c = new myclass<int>();.The rest of your second example should compile OK, however.