Search code examples
c#xnamespace

What is happening when you declare an XNamespace and assign a string value?


Here is the example from MSDN for XNamespace:

XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root", "Content");
Console.WriteLine(root);

I am not sure what is happening in the first line. Is there some kind of implicit conversion going on?


Solution

  • XNamespace has a static Get method that accepts a string parameter and returns an XNamespace instance. So you could rewrite the line as

    XNamespace aw = XNamespace.Get("http://www.adventure-works.com");
    

    In the version you posted, you would be taking advantage of an implicit conversion defined against string. Presumably, the implementation thereof simply invokes the aforementioned method. An example of a possible implementation:

    public static implicit operator XNamespace(string name)
    {
        return Get(name);
    }