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>
myclass
needs a where T : struct
constraint myclass<int> c = new myclass<int>();
. The rest of your second example should compile OK, however.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.