开发者

C# Difference between "using anything" and "system.anything"

开发者 https://www.devze.com 2023-02-16 09:01 出处:网络
Is there any difference between doing this... using System.Xml.Linq; XDocument xml = XDocument.Parse(page);

Is there any difference between doing this...

    using System.Xml.Linq; 
    XDocument xml = XDocument.Parse(page);  

or this...

    System.Xml.Linq.XDocument xml = XDocument.Parse(page);  

Other than the first one makes everything easier to write and cleaner code?

edit: I only used Linq as an example, I'm asking this for ever开发者_运维知识库ything using/system/etc.. related.


There is absolutely no difference in terms of emitted IL. But in the second case you are writing more code which IMHO is not necessary especially when you want to declare multiple XDocument instances in the same class. It's simply useless to be that much explicit when declaring a variable type. Actually an even shorter way would be:

using System.Xml.Linq; 
...

var xml = XDocument.Parse(page);

It's even easier with Shift+Alt+F10+Enter because in this case you only write:

var xml = XDocument.Parse(page);

and Visual Studio automatically adds the proper using after putting the cursor over XDocument.


It's normally okay to just use the "using" method. However, you may have to explicitly qualify a namespace inline if there are namespace conflicts (for example, if you had your own class called XDocument in your namespace)

0

精彩评论

暂无评论...
验证码 换一张
取 消