Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does c# Using Directive affect object size?

Tags:

c#

In other words, does the following make a difference:

this:

using a;
using b;
using c; 

public class foo {
  public void doStuff() {
    // do some stuff utilizing classes from a, b, and c.
  }
}

Versus this:

public class foo {
  public void doStuff() {
    bar.doStuff();
  }
}

together with:

using a;
using b;
using c;

public static class bar {
  public static void doStuff() {
    // do some stuff utilizing classes from a, b, and c
  }
}

Assuming foo is a class that is used and passed around a lot, serialized/deserialized often, etc. However the doStuff() method is called infrequently. Is there any performance advantage at all in breaking it out into the second/third snippets above?

like image 564
mikey Avatar asked Jan 18 '26 20:01

mikey


1 Answers

No. using is merely about which simple names you can use in the source file in which the using declaration appears.

Basically, using N; says to the compiler "hey, if I type the simple name Foo in this source file, and you can't find anything named Foo in scope, can you check if there is anything named Foo in the namespace N?" Thanks!

That is, you can leave off the using and reference everything by it's fully-qualified name and you haven't changed the IL output by the compiler at all.

Is there any performance advantage at all in breaking it out into the second/third snippets above?

Per the above explanation, no.

like image 98
jason Avatar answered Jan 21 '26 11:01

jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!