Skip to content

Commit

Permalink
Enhance Partial Class Documentation with Real-World Example Separatio…
Browse files Browse the repository at this point in the history
…n Across Files. (dotnet#43229)

* Doc enhancement for Partial Class to fix 43217.

* Update docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md

* Fixed CI build error.

* Fixed CI errors.

---------

Co-authored-by: Adit Sheth <[email protected]>
Co-authored-by: Bill Wagner <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent 7a3b35e commit 8b79865
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
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
Expand Up @@ -6,6 +6,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>PartialClassesAndMethods</RootNamespace>
<StartupObject>WrapCoords2.TestCoords</StartupObject>
</PropertyGroup>

</Project>
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

0 comments on commit 8b79865

Please sign in to comment.