0

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.

Community
  • 1
  • 1
Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126

3 Answers3

4

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

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.

Deleted
  • 4,804
  • 1
  • 22
  • 17
0

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
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188