2011-09-07 69 views
0

可能重复:
Nullable type as a generic parameter possible?
Is creating a C# generic method that accepts (nullable) value type and reference type possible?是否有可能限制泛型参数只接受可空类型?

在C#中,我想限制我的泛型方法只接受可空类型。那可能吗?

public T Method<T>() where T : somesortofnullablerestriction 
{ 
    ... 
} 
+1

也看到http://stackoverflow.com/questions/4003026/is-creating-ac-generic-method-that-accepts-nullable-value-type-and-reference和http://计算器。 com/questions/303287/can-a-generic-method-handle-both-reference-and-nullable-value-types –

+0

http://stackoverflow.com/questions/209160/nullable-type-as-a-generic-参数可能 – Ray

+0

该解决方案在我的情况下是无效的,我不想限制为结构。 –

回答

1

你可以这样做。

public Nullable<T> Method<T>() 
{ 
} 
相关问题