Classes and Namespaces

  • Thread starter Thread starter Michael C
  • Start date Start date
M

Michael C

In Vb a class was a file, but in C# you have multiple classes in one file
which
has a namespace.

I am trying to get some common code libraries written but this is stuffing
my head up.

Is it normal in C# to have a namespace for each class file or isn't it a
class

I now need to call like this

<MyNameSpace contained in class file that I added to a solution .
MyClassName> myclassName = new <1st Part>

Is this normal I AM SO CONFUSED (Any help welcome!)

Regards

Michael
 
Michael,

Both c# and vb.net have the idea of a Namespace.
In simple terms, think of a Namespace as a hierarchal organization of your
classes. If you look at the MS .Net Framework, all the classes are
organized by the sort of thing they are used for (kind of like the way you
organize files in a directory structure so you know were to find the
specific file you are looking for without having all files sitting at c:\).
System.Data is all the data stuff, System.IO is all the stream and file io
stuff. We use namespaces in our code for the same reason, just to organize
the classes into something that is easier to deal with.

In C#, each file in the project can have 1 or more classes. Usually, each
file has a Namespace that encloses the class(es) in the file. That same
Namespace can be used in other files as well.

The important part to remember is that Namespaces are not objects, just a
way of organizing things (I expect I will take some flak for that last
statement, but without making things too complicated it is basicly true.)

Kirk Graves
KRGIT Software
 
Hi Michael,
Every class belong to a namespace. This is usefull, because now you may have
classes with equal names, which belongs to different namespaces, and behave
differently without any confusion for the compiler or the user of your
class. As an example, assume that you have 2 namespaces: TCPcomunications
and HTTPcomunications. Now in every namespace you can create
ConnectionClass, and it will be different for evry namespace.
And in your code you can clearly identify the exact class you want to use:
TCPcomunications.ConnectionClass or HTTPcomunications.ConnectionClass.

You can have nested namespaces, like MyCompany.MyFirstProject.BaseObjects.
If you create a class in that namespace - MyClass, this class will be
completeley different from the class MyClass, created by someone else in
other namespace(s), and in your code you can be sure that you are using the
right one.

Also, if you do not want in your code always to type:
MyCompany.MyFirstProject.BaseObjects.MyClass, you can use the "using"
keyword:

using MyCompany.MyFirstProject.BaseObjects

.... and later in the code, you can use:
MyClass obj = new MyClass;

Hope that helps
Sunny
 
Hi

So I could use the using statement as long as the class name didn't exist in
another namespace otherwise I would get a compile error correct (Correct?)

And also if I create a dll should I use the same name as the namespace
within the dll correct!

But what happens if I have the following MyDllName added as a reference to a
solution and
2 namespaces within that Dll ... or is that usually not good coding
practice?

Regards

Michael
 
Back
Top