tags:

views:

97

answers:

4

What is the reason that I have to do both of these for the standard libraries? For every other language that I have used, I only have to do one to access standard libraries.

+1  A: 

Visual studio needs certain attributes of a reference to decide how to resolve it at runtime and how to deploy it when building the project. These options cannot be implied by a simple import statement.

Marco
Could you go into more detail?
Casebash
Sure. I just opened VS and created a new C# project (Windows Forms). The default template for this project references a number of System assemblies. If you right-click and choose properties a property grid opens below, and it details all the various options for dealing with this reference. The most important ones have to do with whether the reference is copied into the project bin folder (and then probably included in a bundled installer), or if the reference should be resolved through the GAC. Also, it determines the type of version binding, i.e. if it requires that specific version or not.
Marco
+1  A: 

You need to reference the assemblies to let the compiler know where to locate the types they export. You don't need to include namespaces, but it makes code easier to read as you can avoid fully qualified names. Additionally since each assembly may expose several namespaces it is a convenient way to group related types.

Brian Rasmussen
+9  A: 

An assembly reference makes the code in the assembly available to another assembly. A using directive makes the types of another namespace available to a given source file. These are completely separate concepts. If you were inclined, you could get away with never using the usings directive and using fully-qualified type names. Similarly, it is possible to use the usings directive to refer to other namespaces within the same assembly.

Also, there is a perfect analog between assemblies/jars and usings/import between Java and C# for the purposes of this discussion. So C# is hardly unique here.

Kirk Woll
Well, the C# compiler could automatically reference all assemblies in the GAC that contain classes in a namespace imported by a `using` directive. This is what Java does (look in all jars in the class path for types in imported packages).
dtb
@dtb, IMO, passing the classpath to your libraries to java.exe is the equivelent of assembly references -- though I do agree with your point that in C# it is granular by project.
Kirk Woll
To avoid confusion: the using **statement** is the one used with IDisposable for deterministic disposal of resources. The using **directive** is the one in the spotlight here.
Martinho Fernandes
@dtb: In C# namespaces and assemblies are independent. It's legitimate to put namespace Foo in both assemblies Foo.Somestuff.dll and Foo.Morestuff.dll. How would you solve that? Add references to both?
Martinho Fernandes
@Martinho, thanks, fixed.
Kirk Woll
@Martinho Fernandes: Yes, the compiler **could** simply reference all assemblies that contain classes in a specific namespace, no matter what the assembly name is. But the designers of .NET chose not to do this.
dtb
@dtb actually the compiler can't do that because you can have multiple versions of the same assembly in the GAC. How does it know which one to pick? It can't include them all because they could have the same types defined in the same namespaces.
Wesley Wiser
+2  A: 

Assembly references are a concept of the platform, while a namespace import is a concept of the language.

You need to give an assembly reference to the compiler because it needs to know where the library code you're using is. But the using directive is purely optional. Both these will compile fine:

Example 1:

System.Console.WriteLine("Without a using directive");

Example 2:

using System;
//...
Console.WriteLine("With a using directive");

The using directive serves only to save you from having to write fully qualified names all over the place.

Martinho Fernandes