我跑步时遇到错误
string quote = Page.RouteData.Values["quote"].ToString() ?? string.Empty;
Run Code Online (Sandbox Code Playgroud)
错误:对象引用未设置为对象的实例.
我知道ToString导致错误,因为Page.RouteData.Values ["quote"]为空/ null.
在执行ToString之前,如何检查Page.RouteData.Values ["quote"]是否有值?
怎么样:
if (Page.RouteData.Values["quote"] != null) {
string quote = Page.RouteData.Values["quote"].ToString() ?? string.Empty;
}
Run Code Online (Sandbox Code Playgroud)
要么
string quote = ((Page.RouteData.Values["quote"] != null) ? Page.RouteData.Values["quote"].ToString() : string.Empty);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5350 次 |
| 最近记录: |