TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# namespace Keyword

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.

Class

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
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?)
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.
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf