Namespaces vs physically coding? [duplicate]

2019-08-16 05:23发布

问题:

Possible Duplicate:
Using Statements vs Namespace path? C#

I just want to know what the difference between including the namespace at the top of a C# class vs actually defining it in the program code.

So:

using System.Windows;

Or

System.Windows.MessageBox.Show();

Would having the namespace load the whole library or will only the needed data be used?

Sorry if it may seem confusing.

回答1:

You need to distinguish between namespaces and assemblies. They're very different things.

using directives only talk about namespaces - and the two ways of referring to MessageBox will produce exactly the same code. Use whichever produces the most readable code - which is usually to use using directives and short names.



回答2:

The first is easier to read. You should favour readability. I don't think there is a compilation or loading benefit either way and if there is I haven't noticed it on 500kloc projects.



回答3:

Write the full namespace directly in your statements if some identifier is ambiguous.

using System.Windows.Forms;
using System.ServiceModel.Channels;

...

var msg = new Message(); // ambiguous
var msg1 = new System.Windows.Forms.Message(); // OK
var msg2 = new System.ServiceModel.Channels.Message(); // OK