I'm getting these down to the tee as I'm into C# and Java right now as they are very similar. My question is what are the key and not so obvious differences between a class and a namespace? The question has been with me for a few days, and I figured I should turn this one over to the gurus.

Recommended Answers

All 3 Replies

I was a little afraid to answer this with my own knowledge cause my answer would be wishy washy.

Namespace:
http://msdn2.microsoft.com/en-us/library/0d941h9d(VS.80).aspx


Classes:
http://www.devarticles.com/c/a/C-Sharp/Introduction-to-Objects-and-Classes-in-C-sharp/1/

From what I have got and part of what I know is classes contain objects. I was taught a class is a blueprint for objects. (Which you probably already know) A namespace is a collection of classes. It also helps to organize classes that have the same name. Mainly for orginization.

If anyone can elaborate more please feel free!

Namespaces offer a way of organizing classes into a logical hierarchy. You can imagine if every class we created were dumped into one bucket. The name collisions alone would be horrendous.

Classes are representations of real (or imagined) world objects that define the implementation details of that object: its properties and methods.

Interfaces figure into this as well. They are definitions of contracts which classes can implement. I won't get into the reasons why these are handy.

So in short, namespaces are useful in organizing classes into some useful hierarchy. This hierarchy can help to organize classes in a way that helps make them more understandable and helps to avoid name collisions. (i.e. two classes with same name can exist in different namespaces).

I am sure I have oversimplified this, but it is a rudimentary explanation. If the coffee was ready, I would probably be more prolific.

So a namespace is there to organize the classes which holds the objects.

Let's go with C#

If I say :

using MagicBeam;
 
public class MagicFu 
{
 
string badDay= Console.Readline();
 
if badDay="yes"
{
//object.method?
Automagic.tportBossToMars(Boss);
}

MagicBeams my namespace right?

Loltastic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.