asp.net-mvcmvchtmlstring

MvcHtmlString.Create() vs new MvcHtmlString()


I see two common ways of creating an MvcHtmlString.

var htmlStr = MvcHtmlString.Create(str);

and

var htmlStr = new MvcHtmlString(str);

Is there any difference between the two?

Can I use them both interchangeably?

Is there every any reason to prefer one over the other?


Solution

  • Here's the actual code according to the JetBrains (R#) decompiler:

    public static MvcHtmlString Create(string value)
    {
      return new MvcHtmlString(value);
    }
    

    So, I think you can be pretty certain there's no difference. I have been consistently using Create thinking there might one day be other static factory methods, and it would be more consistent to always use them, but so far, that hasn't happened.