views:

2156

answers:

4
+2  Q: 

C# partial class

How do I program a partial class in C# in multiple files and in different namespaces?

+13  A: 

You can't. From here ...

Using the partial keyword indicates that other parts of the class, struct, or interface can be defined within the namespace

Must be in the same namespace.

Per comment: Here's an article that discusses defining a namespace cross multiple assemblies. From there ...

Strictly speaking, assemblies and namespaces are orthogonal. That is, you can declare members of a single namespace across multiple assemblies, or declare multiple namespaces in a single assembly.

JP Alioto
Can you have the same namespace over multiple files?
Partial
Yes you can ... over multiple .cs files in the same assembly and over multiple assemblies as well.
JP Alioto
Down-voter has a different opinion?
JP Alioto
Not sure about that downvote. This is correct...
lc
+3  A: 

You can't. A partial class means just that: A single class broken into several files. That also means that all files that this partial class consists of must have the same namespace. Otherwise it wouldn't be the same class anymore.

Joey
Why the downvote? This is correct, especially the "Otherwise it wouldn't be the same class anymore" bit.
lc
+6  A: 

You cannot have a partial class in multiple namespaces. Classes of the same name in different namespaces are by definition different classes.

C. Ross
+4  A: 

A partial class (as any other class) needs to live in one single namespace (otherwise its another class).

To split it between different files just use the partial keyword after the access keyword:

// this bit of the class in a file
public partial class Employee
{
    public void DoWork()
    {
    }
}

//this bit in another file
public partial class Employee
{
    public void GoToLunch()
    {
    }
}
JohnIdol