The above code doesn't declare class Calculator, instead we will be using the classlibrary which contains the declaration of Calculator. But for MyApplication to compile properly, the compiler must be aware of the code in the assembly Calculator. For achieving this we need to give the compiler a reference to the assembly, by giving its name and location.

In Visual studio we can add references to a project by right clicking the project from solution explorer and adding reference to Calculator.dll

Once the reference is added MyApplication will compile properly.

The MsCorLib library

mscorlib is the assembly which contains the Console class. It resides in mscorlib.dll assembly. It also holds C# types and the basic types for most. This assembly is always required because of this Visual studio won't be displaying it under references folder.

Now if we have another Calculator class in say assembly Calculator1.dll.If we use Calculator in MyApplication then a name clash can happen.

Namespaces

Namespace feature will help us to avoid the above problem. Namespaces group a set of types together and give them a name called the namespace name.

Namespace can be any valid identifier. Period can be used to organize types into hierarchies.

1. Start namespace names with the company name.2. Follow the company name with technology name.3. Do not name a namespace with the same name as a class or type4. Every type in a namespace must be different from all the others5. The types in a namespace are called members of the namespace

BCL offers thousands of classes and types for building applications. Namespaces help to organize the related functionality in the same namespace.

Namespace is not closed in a single source file.

Namespace can be a member of another namespace. The member is called a nested namespace.Two types of nesting

Textual nesting

namespace Nested{ namespace Nested1 { }}

Separate declaration

namespace Nested{}

namespace Nested.Nested1{}

Using Directives

Since fully qualified names can be quite long, compiler gives flexibility to shorten it using either using namespace directive or using alias directive. We must be use the using directive on top of the source file before any type declarations. They will be applied for all the namespaces in the current source file.

1 comment:

1.) Start namespace names with the company name.2.) Follow the company name with technology name.I think here you mean a convention and its not required i.e. not a rule rightdigital signatures sharepoint