A bit about the stuff I've done

Thursday 26 April 2012

Why I hate Imports/using

I'm sure other people must find this annoying, not just me.
And even Microsoft do it on MSDN.

What am I on about?

Posting code that requires importing a namespace - without specifying what namespace(s) need to be imported.

Imagine the following code:

Stream stMyStream = new FileStream("SomeFile.txt", FileMode.Open);

Now I know, and you know, that Stream exists within System.IO
but without a using directive at the top of your file, the compiler doesn't know this and it gives you a whole bunch of red squiggly lines.

In the above case it is obvious which namespace you need, but that isn't always so, and you can be left scratchign your head for ages trying to track down which namespace you are missing.

So please people: when you post code example - either use a fully qualified type name, or specify the appropriate namespaces to be used.

System.IO.Stream stMyStream = new System.IO.FileStream("SomeFile.txt", System.IO.FileMode.Open);

Just a note to anyone who doesn't know:
the C# directive
using System.IO;
is equivalant to the VB statement
Imports System.IO

To add further confusion both VB and C# have a using statement with is to do with scoping and disposing of variables.

I use both C# and VB so I tend to interchange terms quite a lot!


No comments:

Post a Comment