This blog is moved to
http://amalhashim.wordpress.com

Saturday, June 6, 2009

C# Namespace And Assemblies - Part 1

Visual studio compilers produces assemblies as output.
Assemblies can be executables as well as class libraries.

Example of a class library

public class Calculator
{
int value1, int value2;
public Calculator(int value1, int value2)
{
this.value1 = value1;
this.value2 = value2;
}

public int Add()
{
return this.value1 + this.value2;
}

public int Add(int value1, int value2)
{
return value1 + value2;
}
}

When we compile this class using a C# compiler to produce a class library (Calculator.dll).

If we want to use Calculator object in say MyApplication

class MyApplication
{
static void Main()
{
Calculator calc = new Calculator(10, 20);
Console.WriteLine(calc.Add());
Console.WriteLine(calc.Add(20, 30));
}
}

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 SampleNamespace
{
TypeDeclarations
}

Namespace can contains period

namespace ABC.Calculator
{
public class Calculator
{
}
}

namespace XYZ.Calculator
{
public class Calculator
{
}
}

Now we can use both Calculator class and avoid the conflict.

In MyApplication we can qualify Calculator class as

ABC.MyCalculator.Calculator calc = new ABC.MyCalculator.Calculator();
XYZ.OurCalculator.Calculator newCalc = new XYZ.OurCalculator.Calculator();

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 type
4. Every type in a namespace must be different from all the others
5. 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.

Example

using System;

so in MyApplication we can use as

Console.WriteLine("") instead of System.Console.WriteLine

Using Sys = System;

Sys.Console.WriteLine("")

1 comment:

Jack said...

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 right
digital signatures sharepoint