Newbie "imports" question

  • Thread starter Thread starter M. Simioni
  • Start date Start date
M

M. Simioni

Perhaps it is a stupid question, but i'm asking you if there is a
difference between using

Imports this.is.a.namespace
....
genericfunction("blablabla")

and directly using in the code

this.is.a.namespace.genericfunction("blablabla")

There are differences between the two piece of codes, about
optimization or something else ? Or they have the same behaviour?
For example, i was thinking if it's good to use imports if i use many
functions of a specific namespace, and directly call a function if i
use namespace's functions only a few times.

Thank you in advance,
Marco
 
Hi Marci,

There is no difference, but importing namespaces instead of using the full
class name makes the code much easier to read.

The drawback are cases where Imports will cause conflicts. If two
difference namespaces contain identical class names you will get a
conflict. For instance defining a Timer object when you have imported
borth System.Timers and System.Threading will confuse the compiler as both
namespace have a Timer class. In this case drop one namespace and use the
full class name for the other. Or define an alias for conflicting classes

Imports System.Timers
Imports ThreadTimer = System.Timers.Timer
 
M. Simioni said:
Perhaps it is a stupid question, but i'm asking you if there is a
difference between using

Imports this.is.a.namespace
....
genericfunction("blablabla")

and directly using in the code

this.is.a.namespace.genericfunction("blablabla")

There are differences between the two piece of codes, about
optimization or something else ? Or they have the same behaviour?
For example, i was thinking if it's good to use imports if i use many
functions of a specific namespace, and directly call a function if i
use namespace's functions only a few times.

Every Import namespace (VB.NET) / using namespace (C#) slows down the
compiler somewhat because it has more candidate names to search. That
doesn't affect jit time or runtime though at all, once the name is resolved
it is stored in IL exactly the same way, with a metadata handle.
 
Back
Top