2010-08-01 163 views
2

我试图实现存储的通用可空类型的类:空类型作为泛型参数

public class myclass<T?> 
{ 
    T? myvariable; 

    public T? myfunction(){ 
     return myvariable; 
    } 
} 

虽然上面的类编译就好了,实际使用时,提供故障:

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. 

我在做什么错,或者我该如何解决这个问题?

+0

你的课不编译。 – dtb 2010-08-01 17:10:16

+0

而你的例子没有意义。你想达到什么目的? – dtb 2010-08-01 17:12:19

+0

事实上,它不会编译......抱歉,其他错误必须在语法检查器中加以说明。 我所追求的是一个存储可空类型的类。 (以上示例中缺少的是变量设置的部分) – Peter 2010-08-01 17:16:25

回答

5

的无论是实施例编译:

然而,第二个例子的其余部分应该编译成OK。

+0

这是解决方案。 非常感谢 – Peter 2010-08-01 17:23:33