I'M trying to learn to program through C#. The book I'M reading is trying to explain namespaces but I'M failing to understand it. As I understand it, a class is a blueprint for an object and, and a class is just a special block of code. So what exactly is a namespace, can I navigate through my windows explorer and see on? Is it actually a C# file, or a collection of C# files? Thanks.

Think of it as an extra layer of misdirection :)

If you code in any kind of organization (or even if you don't), you will inevitably have 180 different Print methods in different classes, some of which will be named Output (just for sake of argument).

Now say 16 divisions got together on a project and pooled all their class resources together into one megaproject. People would be trying to call Print() and getting all kinds of results. You can encase your classes in a namespace (not completely unlike encasing your methods in a class) and be able to tell them apart.

So you can have the output class of production be Production.Output and the Print method be accessed as Production.Output.Print(). If someone wants to use a particular method they can call it by it's fully qualified name. If you know that you're just using the class or method within your own division, you can put a using Production.Output; at the top of your code (conventionally, I suppose it can be anywhere before you call the method, but I may be incorrect about that).

As to your specific question, you can and will have a namespace that crosses multiple files. For instance when you create a winforms application you get a Form1.cs, Program.cs, the design files, etc. that all share a common namespace by default. It's not necessarily a collection of files but for example a certain subset of a dll's classes might have it's own namespace.

Well, hope that gives you something to wrap your brain around. I'm certain I've overlooked some details but I just wanted to give you a run through. Post back with any questions.

Edited 6 Years Ago by jonsca: n/a

Think of it as an extra layer of misdirection :)

If you code in any kind of organization (or even if you don't), you will inevitably have 180 different Print methods in different classes, some of which will be named Output (just for sake of argument).

Now say 16 divisions got together on a project and pooled all their class resources together into one megaproject. People would be trying to call Print() and getting all kinds of results. You can encase your classes in a namespace (not completely unlike encasing your methods in a class) and be able to tell them apart.

So you can have the output class of production be Production.Output and the Print method be accessed as Production.Output.Print(). If someone wants to use a particular method they can call it by it's fully qualified name. If you know that you're just using the class or method within your own division, you can put a using Production.Output; at the top of your code (conventionally, I suppose it can be anywhere before you call the method, but I may be incorrect about that).

As to your specific question, you can and will have a namespace that crosses multiple files. For instance when you create a winforms application you get a Form1.cs, Program.cs, the design files, etc. that all share a common namespace by default. It's not necessarily a collection of files but for example a certain subset of a dll's classes might have it's own namespace.

Well, hope that gives you something to wrap your brain around. I'm certain I've overlooked some details but I just wanted to give you a run through. Post back with any questions.

Suppose I write a small code file and wrap it in a namespace called "MyNameSpace", then I write another file 5 minutes later and the first thing I do is wrap this small code file in a namespace by the name of "MyNameSpace" as well. Would that be a namespace that spanned more than one file? Thanks for your help.

Yes. Though it doesn't mean you can ferret all the files away on your harddisk somewhere and they'll automatically compile or anything. Namespaces still need to end up being part of a particular assembly (search ".NET assemblies" for more info) to make sense.

>So what exactly is a namespace?

From MSDN article:

The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally unique types.

In other words, types are organized into hierarchical structures called namespace, which group related types, and keep groups of types distinct.

>can I navigate through my windows explorer and see on?

No.

>Is it actually a C# file, or a collection of C# files?

No.

A c# program is made up of named entities : namespaces, classes, structs, enums, interfaces and delegates. At a top level, namespaces are entities that organize other entities, such as nested namespaces and types. Types may contains nested types and type members an so on.

Comments
very clear
Much more concise!
This article has been dead for over six months. Start a new discussion instead.