1

Using Resharper 7.1.3, Visual Studio 2012, .net 4:

This code:

    string str = null;
    str = System.Web.HttpUtility.HtmlEncode(str); 

Generates the warning : "possible null assignment to entity marked NotNull attribute". But when I looked at the code for HttpUtility.HtmlEncode using Reflector, it looks like the code ends up checking for a null string, and gets out straight away in such cases. Is this one of those cases where Resharper (?) has got it wrong ? Or is a check such as :

if (str != null)
   str = System.Web.HttpUtility.HtmlEncode(str);   

really necessary ?

Moe Sisko
  • 11,665
  • 8
  • 50
  • 80
  • AFAIK the null check is only done in the second drill through method, `HttpEncoder.Current.HtmlEncode(s)`. The immediate call is to `public static string HtmlEncode(string s)` which doesn't do the null check. You can also use `Code Contracts` or `Assert` to avoid the warning, e.g. `Contract.Requires(str != null)` – StuartLC Oct 31 '14 at 16:59

0 Answers0