哪个是最正确的代码?
if (HttpContext.Current.Response.Cookies[authCookieName] != null) {
HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
}
或
if (HttpContext.Current != null)
if (HttpContext.Current.Response != null)
if (HttpContext.Current.Response.Cookies != null)
if (HttpContext.Current.Response.Cookies[authCookieName] != null)
HttpContext.Current.Response.Cookies[authCookieName].Value = "New Value";
最佳答案
如果 HttpContext、HttpContext.Current、HttpContext.Current.Response 或 Http.Current.Response.Cookies 中的任何一个为 null,那么您就已经有麻烦了。让异常发生并修复您的网络服务器。
关于c# - 检查 Null 异常的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/165458/