Skip to content

Add expression-bodied members examples with parameters #47035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <xref:System.Object.ToString%2A> 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 <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.
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.

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

Expand Down Expand Up @@ -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)]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// See https://aka.ms/new-console-template for more information
ExprBodied.Example.Main();
ExpressionBodiedMembers.Example.Main();
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
// </Snippet1>

public class Example
Expand All @@ -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})");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}"));
}
}
Loading