【发布时间】:2015-03-23 20:49:13
【问题描述】:
我正在将一堆代码从 VB 转换为 C#,但我遇到了一个方法问题。这个 VB 方法效果很好:
Public Function FindItem(ByVal p_propertyName As String, ByVal p_value As Object) As T
Dim index As Int32
index = FindIndex(p_propertyName, p_value)
If index >= 0 Then
Return Me(index)
End If
Return Nothing
End Function
它允许为 T 返回 Nothing(null)。
C# 等效项不起作用:
public T FindItem(string p_propertyName, object p_value)
{
Int32 index = FindIndex(p_propertyName, p_value);
if (index >= 0) {
return this[index];
}
return null;
}
它不会编译并出现此错误:
类型“T”必须是不可为空的值类型,才能在泛型类型或方法
'System.Nullable<T>'中用作参数“T”@
我需要能够拥有相同的功能,否则会破坏很多代码。我错过了什么?
【问题讨论】:
标签: c# vb.net vb.net-to-c#