Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance between importing entire namespace versus using alias in C#?

Is there a performance difference at all between importing an entire namespace versus using aliasing to import only one class? If so, how much of a difference is it?


Examples:

Importing an entire namespace:

using System.Reflection;

Aliasing to import only one class:

using BindingFlags = System.Reflection.BindingFlags;
like image 313
bsara Avatar asked Jul 09 '12 19:07

bsara


People also ask

Should using directives be inside or outside the namespace?

The using directive can appear: At the beginning of a source code file, before any namespace or type declarations.

What is a namespace can we create alias of a namespace?

Namespaces in C# serve two main purposes: to organize classes and functionality and to control the scope of classes and methods. Type aliasing is a little known feature for C# and it comes in handy when you would like to alias a specific method or class from a namespace for the sake of clarity.

What is namespace used for in C#?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

Does C# have import?

In C# you import namespaces, not classes. If the class you are working in is in the same namespace, you don't need to do anything. If its in a different namespace and you are using Visual Studio, just type the name of the class and use the smart tag drop down to add the appropriate using to the top of the class file.


3 Answers

Zero. Namespace imports are a compile-time feature and the generated IL will be exactly the same either way.

like image 138
Alan Avatar answered Oct 07 '22 18:10

Alan


No it is just an help for the programmer, the CIL generated by the compiler is exactly the same.

like image 5
BlackBear Avatar answered Oct 07 '22 16:10

BlackBear


There's no performance difference. One says 'look here when I specify a class name, it might be in here'. The other says 'when I say this class or namespace, I mean use this one'.

like image 3
Kieren Johnstone Avatar answered Oct 07 '22 16:10

Kieren Johnstone