Response.Cookies["MyCookie"].Value warns of null reference exception
I use ReSharper for VisualStudio and开发者_StackOverflow中文版 was wondering why a Response would warrant a null reference exception warning for the following bold text:
HttpContext.Current.Response.Cookies["MyCookie"].Value = "MyValue";
I've always known this to be the way to initialize a new cookie, and would only expect this warning to be generated for Requesting a cookie value. Is ReSharper lying to me? If not, what's going on? Thanks a lot.
Resharper just isn't smart enough to know that behind the scenes this collection will lazily create objects for you.
Request.Cookies will return null if the cookie doesn't exist... but Response.Cookie will create them for you if they didn't exist.
精彩评论