Skip to content

Commit 39b82a0

Browse files
CopilotBillWagner
andauthored
Add expression-bodied members examples with parameters (#47035)
* Initial plan * Add expression-bodied members examples with parameters Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
1 parent c7cde80 commit 39b82a0

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

docs/csharp/programming-guide/statements-expressions-operators/expression-bodied-members.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Expression body definitions can be used with the following type members:
2929

3030
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 <xref:System.Object.ToString%2A> method typically include a single expression that returns the string representation of the current object.
3131

32-
The following example defines a `Person` class that overrides the <xref:System.Object.ToString%2A> 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.
32+
The following example defines a `Person` class that overrides the <xref:System.Object.ToString%2A> 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.
3333

3434
[!code-csharp[expression-bodied-methods](../../../../samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-methods.cs)]
3535

@@ -69,7 +69,7 @@ For more information about events, see [Events (C# Programming Guide)](../events
6969

7070
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.
7171

72-
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.
72+
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.
7373

7474
[!code-csharp[expression-bodied-constructor](../../../../samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-ctor.cs#1)]
7575

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// See https://aka.ms/new-console-template for more information
2-
ExprBodied.Example.Main();
2+
ExpressionBodiedMembers.Example.Main();

samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-ctor.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ public string Name
1515
set => locationName = value;
1616
}
1717
}
18+
19+
// Example with multiple parameters
20+
public class Point
21+
{
22+
public double X { get; }
23+
public double Y { get; }
24+
25+
// Constructor with multiple parameters
26+
public Point(double x, double y) => (X, Y) = (x, y);
27+
28+
// Constructor with single parameter (creates point at origin on axis)
29+
public Point(double coordinate) => (X, Y) = (coordinate, 0);
30+
}
1831
// </Snippet1>
1932

2033
public class Example
@@ -23,5 +36,11 @@ public static void Main()
2336
{
2437
var city = new Location("New York City");
2538
Console.WriteLine(city.Name);
39+
40+
// Examples with multiple constructor parameters
41+
var point1 = new Point(3.0, 4.0);
42+
var point2 = new Point(5.0);
43+
Console.WriteLine($"Point 1: ({point1.X}, {point1.Y})");
44+
Console.WriteLine($"Point 2: ({point2.X}, {point2.Y})");
2645
}
2746
}

samples/snippets/csharp/programming-guide/classes-and-structs/ExpressionBodiedMembers/expr-bodied-methods.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,26 @@ public Person(string firstName, string lastName)
1515

1616
public override string ToString() => $"{fname} {lname}".Trim();
1717
public void DisplayName() => Console.WriteLine(ToString());
18+
19+
// Expression-bodied methods with parameters
20+
public string GetFullName(string title) => $"{title} {fname} {lname}";
21+
public int CalculateAge(int birthYear) => DateTime.Now.Year - birthYear;
22+
public bool IsOlderThan(int age) => CalculateAge(1990) > age;
23+
public string FormatName(string format) => format.Replace("{first}", fname).Replace("{last}", lname);
1824
}
1925

2026
class Example
2127
{
22-
static void Main()
28+
public static void Main()
2329
{
2430
Person p = new Person("Mandy", "Dejesus");
2531
Console.WriteLine(p);
2632
p.DisplayName();
33+
34+
// Examples with parameters
35+
Console.WriteLine(p.GetFullName("Dr."));
36+
Console.WriteLine($"Age: {p.CalculateAge(1990)}");
37+
Console.WriteLine($"Is older than 25: {p.IsOlderThan(25)}");
38+
Console.WriteLine(p.FormatName("Last: {last}, First: {first}"));
2739
}
2840
}

0 commit comments

Comments
 (0)