From c00b34ab346f6f231f0a4f08cf1d6bc0a5d38c3c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 30 Jun 2025 19:42:55 +0000 Subject: [PATCH 1/2] Initial plan From b0903489801dfe76f0b8d46b530c642b53f5c0ef Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 30 Jun 2025 19:52:21 +0000 Subject: [PATCH 2/2] Add expression-bodied members examples with parameters Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../expression-bodied-members.md | 4 ++-- .../ExpressionBodiedMembers/Program.cs | 2 +- .../expr-bodied-ctor.cs | 19 +++++++++++++++++++ .../expr-bodied-methods.cs | 14 +++++++++++++- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/docs/csharp/programming-guide/statements-expressions-operators/expression-bodied-members.md b/docs/csharp/programming-guide/statements-expressions-operators/expression-bodied-members.md index c5d83bfb878aa..d5969b1f9e188 100644 --- a/docs/csharp/programming-guide/statements-expressions-operators/expression-bodied-members.md +++ b/docs/csharp/programming-guide/statements-expressions-operators/expression-bodied-members.md @@ -29,7 +29,7 @@ Expression body definitions can be used with the following type members: An expression-bodied method consists of a single expression that returns a value whose type matches the method's return type, or, for methods that return `void`, that performs some operation. For example, types that override the method typically include a single expression that returns the string representation of the current object. -The following example defines a `Person` class that overrides the method with an expression body definition. It also defines a `DisplayName` method that displays a name to the console. The `return` keyword is not used in the `ToString` expression body definition. +The following example defines a `Person` class that overrides the method with an expression body definition. It also defines a `DisplayName` method that displays a name to the console. Additionally, it includes several methods that take parameters, demonstrating how expression-bodied members work with method parameters. The `return` keyword is not used in any of the expression body definitions. [!code-csharp[expression-bodied-methods](../../../../samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-methods.cs)] @@ -69,7 +69,7 @@ For more information about events, see [Events (C# Programming Guide)](../events An expression body definition for a constructor typically consists of a single assignment expression or a method call that handles the constructor's arguments or initializes instance state. -The following example defines a `Location` class whose constructor has a single string parameter named *name*. The expression body definition assigns the argument to the `Name` property. +The following example defines a `Location` class whose constructor has a single string parameter named *name*. The expression body definition assigns the argument to the `Name` property. The example also shows a `Point` class with constructors that take multiple parameters, demonstrating how expression-bodied constructors work with different parameter combinations. [!code-csharp[expression-bodied-constructor](../../../../samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-ctor.cs#1)] diff --git a/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/Program.cs b/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/Program.cs index eec3e146d76a5..3ae6c746e7ace 100644 --- a/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/Program.cs +++ b/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/Program.cs @@ -1,2 +1,2 @@ // See https://aka.ms/new-console-template for more information -ExprBodied.Example.Main(); +ExpressionBodiedMembers.Example.Main(); diff --git a/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-ctor.cs b/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-ctor.cs index 1eccae545ad87..e43962db2811e 100644 --- a/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-ctor.cs +++ b/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-ctor.cs @@ -15,6 +15,19 @@ public string Name set => locationName = value; } } + +// Example with multiple parameters +public class Point +{ + public double X { get; } + public double Y { get; } + + // Constructor with multiple parameters + public Point(double x, double y) => (X, Y) = (x, y); + + // Constructor with single parameter (creates point at origin on axis) + public Point(double coordinate) => (X, Y) = (coordinate, 0); +} // public class Example @@ -23,5 +36,11 @@ public static void Main() { var city = new Location("New York City"); Console.WriteLine(city.Name); + + // Examples with multiple constructor parameters + var point1 = new Point(3.0, 4.0); + var point2 = new Point(5.0); + Console.WriteLine($"Point 1: ({point1.X}, {point1.Y})"); + Console.WriteLine($"Point 2: ({point2.X}, {point2.Y})"); } } \ No newline at end of file diff --git a/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-methods.cs b/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-methods.cs index b71dd73ea70ab..a11b3f27a94f9 100644 --- a/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-methods.cs +++ b/samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-methods.cs @@ -15,14 +15,26 @@ public Person(string firstName, string lastName) public override string ToString() => $"{fname} {lname}".Trim(); public void DisplayName() => Console.WriteLine(ToString()); + + // Expression-bodied methods with parameters + public string GetFullName(string title) => $"{title} {fname} {lname}"; + public int CalculateAge(int birthYear) => DateTime.Now.Year - birthYear; + public bool IsOlderThan(int age) => CalculateAge(1990) > age; + public string FormatName(string format) => format.Replace("{first}", fname).Replace("{last}", lname); } class Example { - static void Main() + public static void Main() { Person p = new Person("Mandy", "Dejesus"); Console.WriteLine(p); p.DisplayName(); + + // Examples with parameters + Console.WriteLine(p.GetFullName("Dr.")); + Console.WriteLine($"Age: {p.CalculateAge(1990)}"); + Console.WriteLine($"Is older than 25: {p.IsOlderThan(25)}"); + Console.WriteLine(p.FormatName("Last: {last}, First: {first}")); } }