Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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()
    {
    }
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top