When To Use Imports And When To Use The Full Namespace Path ?

  • Thread starter Thread starter Chris Dunaway
  • Start date Start date
C

Chris Dunaway

so my question is when to use the "Imports XXX"

And when to use this kind of statement

The imports statement is just there to save you some typing. I don't
believe it affects performance at all.

I think that you should use the Imports statement to save some time, but
only if it does not make the code ambiguous. Use full or partial
namespaces when it makes the code clearer. Mostly, I think, it is a matter
of personal taste. Others may have alternate opinions.
 
Hi ,

I have an assembly that hold few class and few imports which are not used in
all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....

And in which way the performance will be better ?


Best Regards ,

Tiraman :-)
 
Tiraman,
And in which way the performance will be better ?

Imports is a developer productivity aid, it does not impact runtime
performance at all.

So when to use Imports & when not, depends entirely on you.

Hope this helps
Jay
 
Hi Tiraman,

In some casses it is better not to use the import.

The import imports the namespace in your IDE, that means that if it is a
namespace with a bunch of properties, interfaces whatever your IDE get
totally locked.

My example is MSHTML which make the IDE absolute unusable when an import is
set.

For that it is better to use the Full namespace, (which is also terrible
slow to set for that).

However this are exceptions I assume.

Cor
 
* "Tiraman said:
I have an assembly that hold few class and few imports which are not used in
all of the class's .

so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....

In addition to the other comments: I /always/ import /all/ the
namespaces classes I am using reside in. So it's very easy to see what
namespaces are used by a class by taking a look at the 'Imports'
statements (sorted in alphabetical order). That's my convention...

Just my 2 Euro cents...
 
In addition to the other comments: I /always/ import /all/ the
namespaces classes I am using reside in. So it's very easy to see what
namespaces are used by a class by taking a look at the 'Imports'
statements (sorted in alphabetical order). That's my convention...

Just my 2 Euro cents...

That's funny... I do mine by the length... In other words:

Imports System
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Net.Sockets
Imports System.Runtime.InteropServices
....

And I import everything as well (though, in my case, since I am almost
always using C#, it's actually using but same diff :)
 
Tom,

* Tom Shelton said:
That's funny... I do mine by the length... In other words:

That's what I did for a long time, but then I sorted them
alphabetically. It's just a matter of preference, but alphabetical
order makes finding imports easier, especially if there are > 10
imports.
 
Tiraman said:
so my question is when to use the "Imports XXX"

And when to use this kind of statement

Dim X as New System.XXX ....

And in which way the performance will be better ?

To me, it depends on how much I'm using a namespace.

Am I just using Math.Round() in one or two places? Then I'll type out
System.Math.Round - no big deal. Am I doing an intense printing job?
Then I'll Import System.Drawing.Printing. (And sometimes I do both,
on odd occasions when I want to be clear to a future developer where
something comes from.)

John Fiala
jcfiala523-at-hotmail.com
http://www.livejournal.com/users/fiala_tech/
 
Herfried,
I normally do mine in order of importance, where order of importance does
not matter I use alphabetically.

Order of importance matters where you may have the same Name in more then
one Namespace or Class/Module.

For example:
Imports Microsoft.VisualBasic.Interaction
Imports MyUtilities.MyGenerics

I have an intelligent, overloaded and typesafe IIf in MyUtilities.MyGenerics
while the Interaction.IIf only works with objects. I obviously want to be
certain that MyUtilities.MyGenerics is imported first!

Hope this helps
Jay
 
* (e-mail address removed) (John Fiala) scripsit:
To me, it depends on how much I'm using a namespace.

Am I just using Math.Round() in one or two places? Then I'll type out
System.Math.Round - no big deal. Am I doing an intense printing job?
Then I'll Import System.Drawing.Printing. (And sometimes I do both,
on odd occasions when I want to be clear to a future developer where
something comes from.)

Yep, but even that won't have any influence on execution time.
 
* "Jay B. Harlow said:
I normally do mine in order of importance, where order of importance does
not matter I use alphabetically.

Order of importance matters where you may have the same Name in more then
one Namespace or Class/Module.

For example:
Imports Microsoft.VisualBasic.Interaction
Imports MyUtilities.MyGenerics

I have an intelligent, overloaded and typesafe IIf in MyUtilities.MyGenerics
while the Interaction.IIf only works with objects. I obviously want to be
certain that MyUtilities.MyGenerics is imported first!

OK, that's a good idea and sometimes it's necessary.
 
Hi Tiraman,
effect the performance so my question is why Microsoft didn't put them all
in each page as default so it will save us
the typing and the searching for the namespace ?

I thought that my message did give an answer on this question.
Charles Law could have given that answer as well.

Cor
 
hi every one ,

1st i would like to 10x you all for your answers .
as far as i understand you all most of you use the imports in order to save
some typing but the main issue is that it doesn't
effect the performance so my question is why Microsoft didn't put them all
in each page as default so it will save us
the typing and the searching for the namespace ?

10x again .
 
Tiraman,
How is Microsoft going to decide which to include on in a source? Should it
include EVERY namespace in the Framework? For a class that has 20 lines of
code you would have 100+ lines of imports. Plus as Cor stated, this may
cause problems for the IDE. When Whidbey (VS.NET 2005) is released there
will be a significant increase in the number of namespaces in the Framework.

If you are not using regular expressions, should Microsoft import the
System.Text.RegularExpression namespace? What happens when you have defined
a Match class and the System.Text.RegularExpression.Match class starts
shadowing your Match class?

If you are creating a Windows Forms application, should System.Web.UI be
imported? What happens when I actually use a class from the above namespace
in my Windows Forms app?

Microsoft does not import all the namespaces because you as developer need
to decide which namespaces are appropriate to the class/structure that you
are currently working on.

Remember that namespaces are used to organize the types (classes,
structures, delegates) in the framework & your assemblies, if you
automatically imported every thing, then you really would not need
namespaces and you would suddenly be disorganized!

Take for example the Timer class, without Namespaces which of the three
Timers are you referring to? System.Threading.Timer, System.Timers.Timer,
or System.Windows.Forms.Timer?

The namespace part of the above Timer's name is what makes each class
distinct from the other, without needing to do something funky like
"ThreadTimer", "ServerTimer", "FormTimer", which IMHO is more awkward then
using the Namespace, considering that the namespace groups related types in
a central location (System.Windows.Forms has all the Windows Forms stuff).

Hope this helps
Jay
 
* "Jay B. Harlow said:
Remember that namespaces are used to organize the types (classes,
structures, delegates) in the framework & your assemblies, if you
automatically imported every thing, then you really would not need
namespaces and you would suddenly be disorganized!

Namespaces help finding classes. Importing /all/ available namespaces
doesn't make sense, but importing all namespaces used in the class makes
sense. Sometimes it will be, as you say, necessary to qualify a class
to avoid ambiguity, or alternatively set up an alias for the class name
using 'Imports'.
 
Hi Herfried,

Try once what I said, and set that MSHTML in your import from all your
programs a week, we do not get one message anymore that week when you are
doing that.

And what I did not think about, however thought when I readed the last
message from Jay who said that explicitly, what will happen as well when you
have to much imports from large namespaces.

It makes sense as Jay said when it is possible, as what I did mean with my
message as well.

Cor
 
* "Cor Ligthert said:
Try once what I said, and set that MSHTML in your import from all your
programs a week, we do not get one message anymore that week when you are
doing that.

And what I did not think about, however thought when I readed the last
message from Jay who said that explicitly, what will happen as well when you
have to much imports from large namespaces.

Performance should not be a reason not to follow a certain pattern, but
in some situations there may be performance problems that force me to
not import the namespace.
 
Herfried,
When I stated "imported every thing" I was referring to all 120+ namespaces
in the framework. ;-)

I agree, I normally import the 4 or 5 specific namespaces that I need in a
single class. I will occasionally import either the class (System.Math for
example) or use an Alias (Outlook for example).

Jay
 
Back
Top