我有一个简单的方法:
public static T GetValue<T>(SqlDataReader reader, int columnIndex, T defaultValue = default(T))
{
return reader.IsDBNull(columnIndex) ? defaultValue : (T)reader[columnIndex];
}
及其用法:
string s = SqlUtils.GetValue<string>(reader, nameOrd);
我问自己,为什么我必须指定<string>
从用法中是否清楚返回参数的类型是字符串?但显然我必须这样做,否则编译器会抱怨The type arguments cannot be inferred from the usage...
。我的逻辑在哪里失败?