tags:

views:

173

answers:

6

I am a Java developer, totally new to C#. I am currently writing a DLL for distribution across my organization. It is a very simple library containing a couple of classes and I do not see any real use in putting all of them into some namespace just for the sake of it. Do I really have to use a namespace? If so, why? Is it some kind of a best practice?

+9  A: 

Do you need one? No. Should you have one? Yes. It'll help prevent clashes with identically named classes in other namespaces without having to resort to the (IMHO) ugly use of global::.

Dave
Besides, the concept is easy to grasp and implement, so there is no real good reason to avoid namespaces.
Raveline
+5  A: 

For throwaway test apps (e.g. checking Stack Overflow answers), I don't use a namespace. For anything else, I do. It's just an organization thing - if you're going to reuse code, it's helpful to separate it from other code you're also reusing in the same context. What I mean is, if you're creating an app using LibraryX and LibraryY, it's useful to be able to differentiate between them within the app. It's possible that they both use the same class names, for example - which will make the code ugly if you don't use namespaces.

Aside from anything else, if you're coding with Visual Studio it's actually more work not to include a namespace - you've got to modify the project to give it an empty default namespace.

Jon Skeet
+1  A: 

There is no need to have a namespace. However developer studio expects you to be using a name space. For example, when you choose to add a class to a project developer studio will:

  • Create a file for the class
  • Add the file to the project
  • Create an empty class (in the above file) that is in the project’s default namespace.

A “project’s default namespace” is a developer studio concept not a C# concept and is set in the properties of the project.

As you are creating a dll for others to use, it will be a lot easier for the users of your dll if you have a name space:

  • People expect you to have a namespace (so may be confused if you don’t)
  • Namespaces make it a lot easier for your users if you have class (or enum etc) that is named the same as another class in any dll they are linking to.

Therefore I don’t see a good reason not to use a namespace.

Ian Ringrose
+1  A: 

My vote for "yes" i think it is good habit to use namespace. you can not be sure that people won't use same class names.

jebberwocky
+1  A: 

To respond to your comment about naming a class the same as it's namespace, read a little bit of the following article.

Short version: don't do that.

http://blogs.msdn.com/b/ericlippert/archive/2010/03/09/do-not-name-a-class-the-same-as-its-namespace-part-one.aspx

Josh Smeaton
+1  A: 

Well may good answers are already in place but for more knowledge please refer

a) Namespace

b) Namespace (computer science)

c) Namespaces

d) namespace (C# Reference)

priyanka.sarkar_2