Wayne
Wayne

Reputation: 681

Should I use System.Guid.NewGuid() or using System and then Guid.NewGuid()?

When should I use full name, Sytem.Guid.NewGuid();? Should I always use using System; and then Guid.NewGuid(); for all cases?

Upvotes: 0

Views: 509

Answers (6)

HCL
HCL

Reputation: 36785

I personally don't like this long identifiers. The code is very hard to read if you have a lot of them.
However, when there are ambiguities between type names, the fully qualified version resolves this. I personally only use them when I have to, due to namespace conflicts. And also in this case I like more to declare a namespace aliase. This makes the code much more readable.

Anyway, for the compiled app, it makes no difference, the compiled code is the same.

What I also have encountered, that they were unpractical for some mannual refactoring action, but maybe the opposite may also be true, I don't remember the exact case...

Upvotes: 1

Nicola Musatti
Nicola Musatti

Reputation: 18236

I'd say consistency is more important than which alternative you choose. Personally I tend to always specify using directives and keep them sorted alphabetically, so it's really immediate to see what is or isn't there. Then in my code I always use unqualified names, except when I need to disambiguate between classes with the same name.

Upvotes: 1

Haris Hasan
Haris Hasan

Reputation: 30117

I think it will make more sense to use fully qualified name i.e. Sytem.Guid.NewGuid() if you have duplicate names at some level of class/namespace hierarchy which you want to avoid by explicitly telling the full name.

As System is pretty much unique namespace you should go for Guid.NewGuid()

Upvotes: 1

Adeel
Adeel

Reputation: 19238

you should use the later, i.e. include namespace first. The advantage of it is by only seeing the using statements, you will be well aware that which libraries are used in this file.

Upvotes: 1

Baz1nga
Baz1nga

Reputation: 15579

Namespaces are a compile-time only feature of C# that allow you to save time during development. The using directives are utilized by the compiler to look up shorthand Type names in your code.

Basically each time the compiler encounters a type name in your code that it does not know it takes each using directive and prepends it to the type's name and sees if that fully qualified name resolves.

Once you application is compiled the namespaces and the using directives are gone as the IL does not need them.

To answer your question it really doesnt matter.. if you are using it often in a single file then import it else use the fully qualified namespace

Upvotes: 0

Joachim VR
Joachim VR

Reputation: 2340

Doesn't really make a difference, I think. 'Using' is more useful when coding, but when compiling to IL, all classes get compiled to their full name.

Upvotes: 0

Related Questions