Skip to content

Commit 8b79865

Browse files
shethaaditAdit ShethBillWagner
authored
Enhance Partial Class Documentation with Real-World Example Separation Across Files. (#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]>
1 parent 7a3b35e commit 8b79865

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ There are several situations when splitting a class definition is desirable:
1919
- 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.
2020
- [Source generators](../../roslyn-sdk/index.md#source-generators) can generate extra functionality in a class.
2121

22-
To split a class definition, use the [partial](../../language-reference/keywords/partial-type.md) keyword modifier, as shown here:
22+
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.
23+
24+
The following `Employee` example demonstrates how the class might be divided across two files: Employee_Part1.cs and Employee_Part2.cs.
2325

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

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

8789
## Examples
8890

89-
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.
91+
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.
9092

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

docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/PartialClassesAndMethods.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<RootNamespace>PartialClassesAndMethods</RootNamespace>
9+
<StartupObject>WrapCoords2.TestCoords</StartupObject>
910
</PropertyGroup>
1011

1112
</Project>

docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
//<Snippet1>
2+
// This is in Employee_Part1.cs
23
public partial class Employee
34
{
45
public void DoWork()
56
{
67
}
78
}
89

10+
// This is in Employee_Part2.cs
911
public partial class Employee
1012
{
1113
public void GoToLunch()
1214
{
1315
}
1416
}
17+
18+
//Main program demonstrating the Employee class usage
19+
public class Program
20+
{
21+
public static void Main()
22+
{
23+
Employee emp = new Employee();
24+
emp.DoWork();
25+
emp.GoToLunch();
26+
}
27+
}
28+
29+
// Expected Output:
30+
// Employee is working.
31+
// Employee is at lunch.
32+
1533
//</Snippet1>
1634

1735
//<Snippet2>
@@ -87,6 +105,7 @@ partial class NestedClass { }
87105
namespace WrapCoords2
88106
{
89107
//<Snippet9>
108+
// This is in Coords_Part1.cs
90109
public partial class Coords
91110
{
92111
private int x;
@@ -99,6 +118,7 @@ public Coords(int x, int y)
99118
}
100119
}
101120

121+
// This is in Coords_Part2.cs
102122
public partial class Coords
103123
{
104124
public void PrintCoords()
@@ -107,6 +127,7 @@ public void PrintCoords()
107127
}
108128
}
109129

130+
// Main program demonstrating the Coords class usage
110131
class TestCoords
111132
{
112133
static void Main()

0 commit comments

Comments
 (0)