ForeverZer0
ForeverZer0

Reputation: 2496

Static Using Performance Penalty?

I was curious if there was any runtime performance penalty between importing a static class with a using vs importing the "typical" way and invoking methods explicitly with a receiver, specifically when it comes to a rather large class.

For example, my current project uses OpenGL, where each OpenGL function is bound to a static method in the Gl class. I have chose to keep the naming convention within this class the same as its native counterpart (i.e. glClear is Gl.glClear).

I then use the class like this:

using static OpenGL.Gl;

void SomeFunction()
{
    glClear();
    // etc, etc
}

...as opposed to this...

using OpenGL;

void SomeFunction()
{
    Gl.glClear();
    // etc, etc
}

Naming convention aside, is there any runtime penalty importing so many symbols into the current context in this situation (being the Gl class has hundreds of static methods and public constants)?

I am not really concerned with build-time or the extra workload on the IDE to provide intellisense, only with the final result after compilation.

My feeling is that it is simply syntactic sugar and results in the same IL code, but I am not very well versed on reading IL to confidently determine this.

Upvotes: 1

Views: 122

Answers (1)

Julian
Julian

Reputation: 36710

It's the same as the compiler rewrites it. The generated intermediate language code doesn't even know "usings”.

See https://www.codeproject.com/articles/1119847/csharp-compiler-implementation-of-csharp-features#use_sta

using static

...

If you know CIL, you know that the concept of namespaces do not really exist in CIL. Type and member names are usually resolved to their fully-qualified names. So, using static is simply a compile-time syntactic sugar to make us even lazier than we already are.

PS. CIL, IL and MSIL are the same in this context

Upvotes: 6

Related Questions