C# namespace Keyword (Missing a Using Directive)

Consider the use of namespaces. See namespaces in directives and nest them.
Namespace. A namespace is an organization construct. It helps us find and understand how a code base is arranged. Namespaces are not essential for C# programs.
Namespaces make code clearer. But if you encounter an "Are You Missing a Using Directive" error, a namespace may be involved. A simple fix is possible.
This example has namespaces with the identifiers A, B, C, D, E, and F. Namespaces B and C are nested inside namespace A. Namespaces D, E, and F are all at the top level.

Program: In the Program class, notice how the Main entry point uses the CClass, DClass, and FClass types.

And: Because the using A.B.C and using D directives are present in namespace E, the Main method can directly use those types.

Also: With FClass, the namespace must be specified explicitly because F is not included inside of E with a using directive.

C# program that demonstrates namespace use using System; using A.B.C; namespace E { using D; class Program { static void Main() { // Can access CClass type directly from A.B.C. CClass var1 = new CClass(); // Can access DClass type from D. DClass var2 = new DClass(); // Must explicitely specify F namespace. F.FClass var3 = new F.FClass(); // Display types. Console.WriteLine(var1); Console.WriteLine(var2); Console.WriteLine(var3); } } } namespace A { namespace B { namespace C { public class CClass { } } } } namespace D { public class DClass { } } namespace F { public class FClass { } } Output A.B.C.CClass D.DClass F.FClass
Allowed names. In addition to using the normal alphanumeric names for namespaces, you can include a period in a namespace. This is a condensed version of having nested namespaces.
Missing error. Every C# programmer will have encountered this error at some point. The compiler says "Are You Missing a Using Directive."

Tip: You usually are missing a "using directive." But figuring out the right one to add is not always easy.

Note: Find a squiggly red line in Visual Studio under a type name. Then try to find its namespace—add that (like Test).

C# program that causes compile-time error // using Test; class Program { static void Main() { // We need to have a using statement "using Test" to compile. Option option = new Option(); } } namespace Test { class Option { public int value; } } Output Error CS0246 The type or namespace name 'Option' could not be found (are you missing a using directive or an assembly reference?)
Execution. There is no performance impact with a namespace. Namespaces do not add complexity to the code itself, just to how it is organized in the source files.

So: At the level of the intermediate language, the namespaces add no computational burden.

However: Namespaces will increase the size of the executable file and the metadata. This is not usually worth considering.

Conceptually, namespaces are an organizational construct that we use to change the way code is arranged. Using namespaces, we provide hints about ownership, responsibility and intent.
A namespace provides an extra layer of information about the source text. This is important. Other people (besides you, the person writing the code) may need to understand it someday.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls