在我的方法中,我希望处理可为空的整数值。
我们不想编写
public String FormatValue(Int32? item)
{
if (item.HasValue==false) return "";
return item.Value.ToString("### ### ###");
}
我们不想编写
Int32?
,Int64?
和其他版本的方法,因此我们希望通过重构来处理泛型类型参数:public String FormatValue<T>(T item)
{
if (item.HasValue==false) return ""; // ERROR: no .HasValue property
return item.Value.ToString("### ### ###"); // ERROR: usually has no ToString() with string argument
}
我该如何处理这种情况?我尝试使用 "where T"
子句,但并没有成功。