有没有办法写这个更紧凑?
return _searchRedirectionMap.ContainsKey(query) ? _searchRedirectionMap[query] : "";
Run Code Online (Sandbox Code Playgroud)
Givent _searchRedirectionMap被定义为IDictionary<string,string>
您可以编写一个IDictionary使用该TryGetValue方法的扩展方法:
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key, TValue defaultValue)
{
TValue outValue;
if (source.TryGetValue(key, outValue))
{
return outValue;
}
return defaultValue;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它:
return _searchRedirectionMap.GetValueOrDefault(query, string.Empty);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
259 次 |
| 最近记录: |