views:

1095

answers:

3

In C# some of default name space such as System.Collections are listed without typing in using blah. In visual basic, they are not imports for you. Is there a way to force vb to auto imports some of default name space or VB work differently than C#?

+2  A: 

I'm not sure what you're asking. I can see potentially two questions there:

  1. Can you change the VB auto-imports?
  2. Can you get auto-import behavior in C#?

For #1, yes you can. Assuming Visual Studio 2005 or higher, go into your project properties, and select the References tab. The auto-imports are listed under "Imported Namespaces" at the bottom of the view.

For #2, not that I'm aware of. I've never seen that behavior in Visual C#.

John Rudy
+4  A: 

I think the first item posted by John Rudy is what you're looking for- add them in the project properties.

However, VB.Net does also work differently than C#, in that it means a different thing in VB to import a namespace than it does in C#. When you import a namespace in VB, it also brings child namespaces 'in scope', in a manner of speaking.

Take the System namespace, for example, which is imported by default. Because the System namespace is imported, you don't have to first type System. to reference a child namespace like IO, like you would in C#. So, right out of the box you can say something like this in VB:

If IO.File.Exists(MyFile) Then ....

That just isn't possible in C# right now. You either have to also import System.IO and then just say File.Exists() or list out the System namespace as well: System.IO.File.Exists().

It may not seem very significant, but you really get used to this VB feature after a while, and it comes in handy more than you'd think. I bring all this up because the end result of this feature is that you often don't want to import as many namespaces in VB as you do in C#.

Joel Coehoorn
You're correct. My mindset is fixed on how C# does things.
Jack
A: 

I don't believe Visual Studio does this by default for C#or VB.

However, the Jetbrains Resharper plugin will auto import namespaces for you, in the same that Eclipse does when you use CTRL+SHIFT+O.

Jon