user34537
user34537

Reputation:

C# How do i define methods for the same class in mutliple files?

I have a class that is getting to be large. How do i define it so methods can span across different .cs files?

Upvotes: 2

Views: 235

Answers (4)

Natrium
Natrium

Reputation: 31174

You can use a partial class for this.

http://msdn.microsoft.com/en-us/library/wa80x488(VS.80).aspx

Employee_1.cs

public partial class Employee
{
    public void DoWork()
    {
    }
}

Employee_2.cs

public partial class Employee
{
    public void GoToLunch()
    {
    }
}

Upvotes: 7

cjk
cjk

Reputation: 46435

It might be nice having multiple smaller files, but it will be more of a pain to find the correct code when its split across two files.

Upvotes: 1

Kirschstein
Kirschstein

Reputation: 14868

Use the partial keyword.

Foo.cs

public partial class Foo 
{
   .....
}


xFoo.cs

public partial class Foo
{
 ......
}

However, if your class is getting large, it's probably a bit of a code smell and time to think about refactoring.

Upvotes: 3

Konrad Rudolph
Konrad Rudolph

Reputation: 545638

You can declare your class as partial. This allows you to split it over multiple files.

// File 1:
partial class Test {
    public void Method1() { … }
}

// File 2
partial class Test {
    public void Method2() { … }
}

Upvotes: 10

Related Questions