tags:

views:

163

answers:

9

Well I couldn't find any previous posting to answer my question so....

I am new to C# and creating some Windows Forms and noticed that it created a both Program.cs and Form1.cs files.

In both, it starts with the namespace of my program "Contacts"

namespace Contacts
{
   //code here

Are these compiled together, or are they still seen separately by the compiler?

+6  A: 

namespace is just a prefix to class name, the way to separate classes. they are compiled separately.

Andrey
+4  A: 

Yes, you can. The Contacts namespace will contain all classes defined across files that define that namespace.

You can also define types belonging to different namespaces in the same file. Files and namespaces are completely orthogonal concepts.

You can also split a class definition across multiple files, since C# 2.0. See here.

Mau
A: 

There is no problem with splitting a namespace across multiple files, and in most programs you will do exactly that.

Typically all of the .cs files will be compiled together with the C# compiler (csc).

You can see how your code is compiled by changing this setting:

Tools | Options | Projects and Solutions | Build and Run

Change the drop down: "MSBuild project build output verbosity" to one of the higher settings. It is set to minimum by default.

Brian R. Bondy
A: 

The short answer is yes, you can split them across separate files.

Evan Trimboli
+4  A: 

From the MSDN Documentation:

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.

Yes, namespaces can (and usually are) split across multiple code files. The classes in those namespaces are compiled separately, but (generally, leaving out external resources for now) into a single output file (i.e. an exe or a dll).

In a very broad sense, think of it like sorting laundry. Each "pile" (the pile of colors, the pile of whites, etc.) would be a namespace. Each piece of clothing in a pile would be a class or an interface.

Hope that helps a little...

Wonko the Sane
Better version of the analogy: The piles themselves are the files, but you can have multiple piles of whites, multiple piles of colors, etc., and they're each grouped together with like types of piles. ...Though I forget if C# requires one class per file like Java does.
JAB
You can put multiple classes in one file (although it makes maintenance more difficult if you are looking for a class in a file that is not named after that class). You can also split the same class across multiple files using partial classes (this is what is done by default for, say, WPF window classes, hiding the GUI initialization glue code).
Wonko the Sane
A: 

Yes, you can use the same namespace in multiple files.

The most accessible example: in Visual Studio, after you create your project, you can create more files in that project. There is a setting for the project for what default namespace to assign to new files. Your project will be compiled as a single dll/exe.

You can also use existing namespaces like System. Your System class will be in your assembly. .NET's main assemblies containing System stuff will not be recompiled to include your addition. However, you still will only need 1 using System statement at the top of classes to use your new System.x class.

Note: I am NOT advocating putting all of your code into System so you can avoid using statements later. There are very strong reasons to not do this but it's overkill for purposes of answering the original question.

Dinah
A: 

Both are compiled into 1 namespace, Contacts. It's rather normal you have multiple files and a single namespace.

A namespace is just some kind of a wrapper around your classes. For example, now you can have multiple classes with the name Sentence. You can create a Sentence in the namespace Jail and one in the namespace Linguistics.

This way you'll have 2 representations of 1 class name, Jail.Sentence and Linguistics.Sentence. Both also have a different body.

I use my namespaces to 'organize' the code a bit. All of my interfaces exist in Projectname.Contracts and my normal code in Projectname

This Wikipedia page is usefull also: Wiki

Jan_V
A: 

Yes, you can span files and even assemblies with a namespace. Here's another trick:

In Visual Studio, If you go to Solution Explorer and create a folder below your csharp project, that folder name will be added by default to the namespace for any file you create in the folder.

Example:

  1. Create a new csharep project.
  2. right-click on the project, and choose properties
  3. Set the default namespace (in the properties dialog) to "MyProject"
  4. Ok
  5. Now create a new class in your project.
  6. Notice that the class's namespace is "MyProject"
  7. Now, right-click your project and create a folder named "ASubNamespace"
  8. Now right-click that folder and choose add | class
  9. Notice that your new class is in the MyProject.ASubNamespace namespace!
JMarsch
A: 

If you aren't splitting it across different files, you are probably being overly fine in your namespace layout.

Jon Hanna