MvcHtmlString.Create() vs new MvcHtmlString()

时间:2016-04-04 17:32:57

标签: asp.net-mvc 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?

1 个答案:

答案 0 :(得分:3)

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.