C# FAQ: Why add a using statement and a reference
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:FAQs |
| edit |
Why add a using statement and a reference?
Why must a reference be added to a project when a using statement is already in effect? The answer is related to the fact that the using statement and assembly references have two different purposes.
The purpose of a namespace is to provide for the logical organization of types and for type reference disambiguation. When a namespace is specified in a using statement, the C# compiler knows that the types in that namespace are to be used in an unqualified fashion, e.g. xxx. The main point is that namespaces are logical entities that might exist in one or more assemblies.
Assemblies—on the other hand—are physical entities containing multiple types. In answer to this question, the relevant point about assemblies is that they are the .NET deployment units.
Therefore, the reason why a reference to the assembly in which a type is located must be added is so that the C# compiler can find the physical location of that type. This would not be possible using only the logical namespace provided by a using statement.