From 64f379c30b6d61f3c1db362ef2813b4b8c1414e2 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Mon, 28 Oct 2024 15:15:41 -0700 Subject: [PATCH 1/4] Doc enhancement for Partial Class to fix 43217. --- .../partial-classes-and-methods.md | 6 ++++-- .../partial-classes-and-methods/Program.cs | 21 +++++++++++++++++++ 2 files changed, 25 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..8714e7afa70f4 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/Program.cs b/docs/csharp/programming-guide/classes-and-structs/snippets/partial-classes-and-methods/Program.cs index efd7925190bbb..dfe1e9dd77862 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() From a9d5841a970854c95557f16e1bbbd20ed324cf42 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 29 Oct 2024 09:41:32 -0400 Subject: [PATCH 2/4] Update docs/csharp/programming-guide/classes-and-structs/partial-classes-and-methods.md --- .../classes-and-structs/partial-classes-and-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 8714e7afa70f4..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 @@ -21,7 +21,7 @@ There are several situations when splitting a class definition is desirable: 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. +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"::: From 2b915b239339d84906be9f5ae166eb16a8417fed Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Tue, 29 Oct 2024 09:48:40 -0700 Subject: [PATCH 3/4] Fixed CI build error. --- .../partial-classes-and-methods/Program.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 dfe1e9dd77862..8f63b1d8b6b1b 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 @@ -16,15 +16,15 @@ 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(); - } -} +// public class Program +// { +// public static void Main() +// { +// Employee emp = new Employee(); +// emp.DoWork(); +// emp.GoToLunch(); +// } +// } // Expected Output: // Employee is working. From d31e977313e6442b1646307f69d6af82dbf3b7f1 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Tue, 29 Oct 2024 09:53:44 -0700 Subject: [PATCH 4/4] Fixed CI errors. --- .../PartialClassesAndMethods.csproj | 1 + .../partial-classes-and-methods/Program.cs | 20 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) 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 8f63b1d8b6b1b..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 @@ -15,16 +15,16 @@ 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(); -// } -// } +//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.