更新时间:2023-02-14 17:00:01
如果您指定了默认值返回值,而不是使用默认值(T)?
What if you specified the default value to return, instead of using default(T)?
public static T GetQueryString<T>(string key, T defaultValue) {...}
它使调用变得更简单:
var intValue = GetQueryString("intParm", Int32.MinValue);
var strValue = GetQueryString("strParm", "");
var dtmValue = GetQueryString("dtmPatm", DateTime.Now); // eg use today's date if not specified
缺点是需要魔术值来表示无效/缺失查询字符串值。
The downside being you need magic values to denote invalid/missing querystring values.