2

Say I have foo.cs and bar.cs. if I create two seperate projects for each source file and compile them to two DLLs, I have no problem in importing and using them in other projects.

What should I do if I want an output of single DLL?(with keeping separate source files) Can a DLL file have two namespaces? if not, How can I place contents of foo and bar into single namespace? (I can edit them)

thkang
  • 11,215
  • 14
  • 67
  • 83

6 Answers6

5

You only have to add both files to the same Project.

Can a DLL file have two namespaces?

Yes. And conversely one namespace can be used in multiple DLLs.

H H
  • 263,252
  • 30
  • 330
  • 514
5

if I create two seperate projects

Create one project with both files in it to produce one DLL as output. The namespaces can be anything you'd like, though convention suggests that classes within a project share a common root namespace.

David
  • 208,112
  • 36
  • 198
  • 279
2

See ILMerge.

Microsoft says:

ILMerge is a utility that can be used to merge multiple .NET assemblies into a single assembly. It is freely available for use from the Tools & Utilities page at the Microsoft .NET Framework Developer Center.


The namespaces are completely independent from the source files and projects. You can have several namespaces in one project or even in one file as well as one namespace for several projects. And of cause you can have several source files in one project (you almost always have many source files per project unless it's a "Hello World"-project)

You can change the default namespace given to new source files in the project properties in the "Application" tab, field "Default namespace". You can also create new folders and subfolders in your project. The name of these folders is automatically added to the default namespace of new source files created within these folders (separated by dots .). And of cause you can always edit the namespace-statements manually. You add or remove and nest namespace statements.

You can also have several projects in one solution. You can even mix projects of different types and languages (e.g. VB and C#) within one solution. Every project usually generates one assembly (dll or exe). It makes no difference whether the projects are within the same solution or not from the technical perspective. It's only a matter of organization.

See:

Example of a complex solution:
Example of a complex solution

Community
  • 1
  • 1
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

C# namespaces are quite open.

Yes, you can have several namespaces in one library.

But also: you can add things into an existing namepace. For example: you could add extention methods into the System.Linq namespace, so that you only have to include the dll without requiring additional includes.

René Wolferink
  • 3,558
  • 2
  • 29
  • 43
0

You can have multiple child namespaces within a single project. It might be better practice to at least give the project (and, once compiled, the DLL) an overarching namespace, though.

devstruck
  • 1,497
  • 8
  • 10
0

If you only need to merge a single class in these files you can use partial classes

But they have to be in the same namespace anyway.

Yakup Ünyılmaz
  • 404
  • 3
  • 11