What is the difference between a class library and a namespace?

What is the actual difference between a class library and a namespace? I know that both are used to group classes, namespaces, etc. Can anyone tell me in which scenario I should use the class library and when to create a new namespace.

+7
source share
2 answers

Namespaces provide conditional separation for classes, class libraries provide physical separation (in Windows they consider a standalone dll).

Class libraries are useful when you want to combine functions that can be shared with other projects.

Imagine that you are creating a Windows application in which you separate the user interface and implementation classes (class library) into two different projects. Then you will find that you need a web version of the same. You can simply import the class library created from the Windows application, and you have all your implementation available to you, and just focus on the web interface.

If you created the entire Windows application in one project, using the namespace to separate them, doing it would be complicated and messy. (ever tried to import exe?)

It is worth noting that class libraries themselves will use namespaces to provide further conditional division into classes within them.

+8
source

The purpose of namespaces is to avoid name conflicts. The namespace should be used if you have several hundred classes. Another indicator is when your code is being used by someone else, and your names may interfere with the names in the user code.

Another important difference is that a class is always a data type. You can define variables with this type. The namespace is not a type. You cannot define vars with a namespace.

+3
source

All Articles