2011-09-07 79 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>() 
{ 
} 
相關問題