matthewr
matthewr

Reputation: 4739

Namespaces inside a global namespace C#

So I have made a .dll containing some methods that I wish to use in another project. All is going well. I currently have the .cs files inside the .dll set out like this:

GeneralClass.cs
NetworkingClass.cs
TextProcessingClass.cs

And inside each of them is this:

namespace General // The name of each .cs file without 'Class'
{
    ...
}

So I will have these namespaces:

And I can access them in another project by doing:

using General;
using Networking;
...

This is all fine but I was wondering if there was a way to do it like this:

using MyDll.General;
using MyDll.Networking;
...

So everything would be under MyDll, just like System and all of its sub namespaces.

If you can help me, please post here.

Upvotes: 1

Views: 1102

Answers (4)

Mehmet Ali Sert
Mehmet Ali Sert

Reputation: 184

In your dll project, just change

namespace General {}

to

namespace MyDll.General {}

and re-build it

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

If you want namespace to be different why not to just do it? There are no restrictions on namespace to match file name (unlike some other languages).

namespace MyDll.General 
{ 
    ... 
} 

Upvotes: 5

Waqar
Waqar

Reputation: 2591

Change you namespace like

namespace MyDll.General
{
    // define classes here
}

namespace MyDll.Networking
{
    // define classes here
}

namespace MyDll.TextProcessing
{
    // define classes here
}

Upvotes: 1

Habib
Habib

Reputation: 223187

Define namespace as:

namespace MyDll.General

Upvotes: 7

Related Questions