Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ There are several situations when splitting a class definition is desirable:
- You can add code to the class without having to recreate the source file that includes automatically generated source. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
- [Source generators](../../roslyn-sdk/index.md#source-generators) can generate extra functionality in a class.

To split a class definition, use the [partial](../../language-reference/keywords/partial-type.md) keyword modifier, as shown here:
To split a class definition, use the [partial](../../language-reference/keywords/partial-type.md) keyword modifier. In practice, each partial class is typically defined in a separate file, making it easier to manage and expand the class over time.

The following Employee example demonstrates how the class might be divided across two files: Employee_Part1.cs and Employee_Part2.cs.

:::code language="csharp" source="snippets/partial-classes-and-methods/Program.cs" id="Snippet1":::

Expand Down Expand Up @@ -86,7 +88,7 @@ For more information, see [Constraints on Type Parameters](../generics/constrain

## Examples

In the following example, the fields and the constructor of the class, `Coords`, are declared in one partial class definition, and the member, `PrintCoords`, is declared in another partial class definition.
In the following example, the fields and constructor of the `Coords` class are declared in one partial class definition (`Coords_Part1.cs`), and the `PrintCoords` method is declared in another partial class definition (`Coords_Part2.cs`). This separation demonstrates how partial classes can be divided across multiple files for easier maintainability.

:::code language="csharp" source="snippets/partial-classes-and-methods/Program.cs" id="Snippet9":::

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
//<Snippet1>
// This is in Employee_Part1.cs
public partial class Employee
{
public void DoWork()
{
}
}

// This is in Employee_Part2.cs
public partial class Employee
{
public void GoToLunch()
{
}
}

// Main program demonstrating the Employee class usage
public class Program
{
public static void Main()
{
Employee emp = new Employee();
emp.DoWork();
emp.GoToLunch();
}
}

// Expected Output:
// Employee is working.
// Employee is at lunch.

//</Snippet1>

//<Snippet2>
Expand Down Expand Up @@ -87,6 +105,7 @@ partial class NestedClass { }
namespace WrapCoords2
{
//<Snippet9>
// This is in Coords_Part1.cs
public partial class Coords
{
private int x;
Expand All @@ -99,6 +118,7 @@ public Coords(int x, int y)
}
}

// This is in Coords_Part2.cs
public partial class Coords
{
public void PrintCoords()
Expand All @@ -107,6 +127,7 @@ public void PrintCoords()
}
}

// Main program demonstrating the Coords class usage
class TestCoords
{
static void Main()
Expand Down
Loading