Question

I've inherited an old .NET 2.0 C# system in work, currently sifting my way through an enormous code base. As I am a graduate I'm interested in why the existing developers did certain things, certain ways. One particular habit the previous developers had was, instead of importing the references at the top of the class like so -

using System.IO;

They did this continually throughout - (rather than importing the reference at the top).

System.IO.File.Exists();

Could anyone shed some light what the difference(s) is/are other than having to type more code? The system I'm working on is a business object orientated system (CSLA), and with no prior experience to this methodology, could someone recommend a good way to approach learning a system which I've inherited. I appreciate you can't see the system I've got but a bit of insight from someone experienced would be appreciated.

Regards.

Was it helpful?

Solution

It's just a style choice. Some people like to use the full names to know that local type names won't conflict with system types.

using statements are just a way to help the compiler find the referenced types at compile time, there is no difference at runtime between;

using System.IO;

File.Exists();

and

System.IO.File.Exists();

OTHER TIPS

Could anyone shed some light what the difference(s) is/are other than having to type more code?

It's a coding standard / style choice as Joachim says.

I personally use usings for most namespaces but will use fully qualified names if it makes the code clearer in a particular case. Such as to avoid ambiguity.

Further, I've seen some teams use usings for .NET types and fully qualified names for types that they have developed or very specific scarse types that the team is not always aware of. Using fully qualified names states that this type is rare and this is the namespace it's in so you don't have to go looking for it.

Could someone recommend a good way to approach learning a system which I've inherited

Don't try to understand everything up front. Learn what you need to know when you need to know it (when you are making changes). Gather a high level understanding about where things are so you can find them quickly when you need to work on them.

I usually prefer using statements but there are places where it will be ambigious to use it.

Consider the following

namespace MyNamespace
{
    public class File
    {
         public static bool Exists()
         {
             return false;
         }
    }
}

Then use

using System.IO;
using MyNamespace;

File.Exist();//this is now ambigious

In such cases you've to use System.IO.File.Exist();

Or

using System.IO;
using MyFile = MyNamespace.File;
File.Exist();//this is call is not ambigious since File means System.IO.File only

Other than these I don't find any reason to use full name rather than using statements

Personally, I like using the full name if I'm only using something in that namespace once or twice in the class. This way, it doesn't clutter up IntelliSense, and it helps me focus on the namespaces I actually care about in that particular class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top