Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, is it more performant to use fully qualified names vs the 'using' directive?

In C#, when you add a using directive for a namespace, it gives you access to all the types in that specific namespace. However, if the namespace has a lot of types and I only need one particular one, I often just use the fully qualified name thinking that I don't want to make available any unnecessary classes I know I am not going to use (especially if there are a lot of them in that namespace) for performance reasons. I was thinking that there has to be some impact to performance (no matter how minute) to make them available as opposed to not, but how much? (if there actually is one). And if so, would it then be bad practice to do this all over the place, because wouldn't it then start to accumulate to something noticable (performance wise)?

I did see the other SO post about using the using directive vs fully qualified names, but it wasn't in reference to performance.

like image 877
johntrepreneur Avatar asked Mar 13 '13 17:03

johntrepreneur


People also ask

What is '~' in C language?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What does the || mean in C?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What does the operator do in C?

C language supports a rich set of built-in operators. An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. Operators in programming languages are taken from mathematics.

What does -= mean in C++?

-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C - A.


2 Answers

The using directive is only syntactic sugar that disappears during compilation. Whether or not the namespace was included via using or mentioned in a fully-qualified type name is totally irrelevant in the resulting bytecode. Hence, there is no performance benefit at runtime by using one or the other.

like image 124
O. R. Mapper Avatar answered Oct 19 '22 20:10

O. R. Mapper


There is no difference.

It might have a negligible (i.e. probably not measurable) impact/benefit on compiler performance (as in when running msbuild), but at runtime the IL explicitly knows the type that is intended, as it's baked in the code as a type handle. There is no 'searching' for types unless reflection is being used.

like image 42
Andras Zoltan Avatar answered Oct 19 '22 21:10

Andras Zoltan