Namespaces and Assemblies in C#


This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

Assemblies are for physical scope and namespaces are for logical scope, so namespaces can be expanded over assemblies but the converse is not possible.

The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally unique types.

Even if you do not explicitly declare one, a default namespace is created. This unnamed namespace, sometimes called the global namespace, is present in every file. Any identifier in the global namespace is available for use in a named namespace.

Namespaces are hierarchical. They implicitly have public access, which you cannot modify.

It is possible to define a namespace in two or more declarations. For example, the code in Listing 5.37 defines both classes as part of namespace MyCompany.

Listing 5.37: NameSpace.cs, Namespaces Example


using
System;

// recurring namespaces

namespace
MyCompany.Proj1
{
    class MyClass
    {
    }
}

namespace
MyCompany.Proj1
{
    class MyClass1
    {
    }
}

//nested namespaces

namespace
SomeNameSpace
{
    public class MyClass
    {
        public static void Main()
        {
            Nested.NestedNameSpaceClass.SayHello();
        }
    }

    namespace Nested // a nested namespace
    {
        public class NestedNameSpaceClass
        {
            public static void SayHello()
            {
                Console.WriteLine("Hello");
                Console.ReadLine();
            }
        }
    }
}


The code listing generates the screen output in Figure 5.8.

fig-5.8.gif

Figure 5.8: Screen Output from Listing 5.37

C# programs are organized using namespaces, which serve as both an internal organization system for a program and an external organization system that is, a way of presenting program elements that are exposed to other programs.

C# also enables the definition and use of aliases. Such aliases can be useful in situations in which name collisions occur between two libraries, or when a few types from a much larger namespace are being used. Listing 5.38 presents code from the preceding example, rewritten to use aliases.

Listing 5.38: NameSpace Assignment Example


using
MessageSource = Microsoft.CSharp.Introduction.HelloMessage;
class
Hello
{
    static void Main()
    {
        MessageSource m = new MessageSource();
        System.Console.WriteLine(m.GetMessage());
    }
}


An assembly is everything that comprises a .NET application. It is the unit of deployment for the Microsoft .NET framework and takes the form of an executable (.exe) file or dynamic-link library (DLL). What makes assemblies different from .exe files or DLLs in earlier versions of Windows is that assemblies contain within them all the information that you would find in a type library along with information about everything else that is necessary to use the application or component.

One of the primary goals of assemblies is versioning. Specifically, assemblies provide a means for developers to specify version rules between different software components and to have those rules enforced at runtime.

Because assemblies are the building blocks of applications, they are the logical point for specifying and enforcing version information. Each assembly has a specific version number as part of its identity.

Conclusion

Hope this article would have helped you in understanding Namespaces and Assemblies in C#. See other articles on the website on .NET and C#.
 

visual C-sharp.jpg The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

Up Next
    Ebook Download
    View all
    Learn
    View all