From 8b7986526b22a12dad9fd03939f60bfaba7051e9 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Wed, 30 Oct 2024 07:34:25 -0700 Subject: [PATCH] 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 Co-authored-by: Bill Wagner --- .../partial-classes-and-methods.md | 6 ++++-- .../PartialClassesAndMethods.csproj | 1 + .../partial-classes-and-methods/Program.cs | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md b/docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md index d56e16e8aeb3d..52f1f8c6aed66 100644 --- a/docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md +++ b/docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md @@ -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"::: @@ -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"::: diff --git a/docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/PartialClassesAndMethods.csproj b/docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/PartialClassesAndMethods.csproj index 2fcfbff5947b7..5493b1b90d80f 100644 --- a/docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/PartialClassesAndMethods.csproj +++ b/docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/PartialClassesAndMethods.csproj @@ -6,6 +6,7 @@ enable enable PartialClassesAndMethods + WrapCoords2.TestCoords diff --git a/docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/Program.cs b/docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/Program.cs index efd7925190bbb..0f39465b2b163 100644 --- a/docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/Program.cs +++ b/docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/Program.cs @@ -1,4 +1,5 @@ // +// This is in Employee_Part1.cs public partial class Employee { public void DoWork() @@ -6,12 +7,29 @@ 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. + // // @@ -87,6 +105,7 @@ partial class NestedClass { } namespace WrapCoords2 { // + // This is in Coords_Part1.cs public partial class Coords { private int x; @@ -99,6 +118,7 @@ public Coords(int x, int y) } } + // This is in Coords_Part2.cs public partial class Coords { public void PrintCoords() @@ -107,6 +127,7 @@ public void PrintCoords() } } + // Main program demonstrating the Coords class usage class TestCoords { static void Main()