Skip to content
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
10 changes: 9 additions & 1 deletion docs/csharp/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,20 @@ For example, these two methods use the `return` keyword to return integers:

:::code language="csharp" source="snippets/methods/return44.cs" id="snippet44":::

To use a value returned from a method, the calling method can use the method call itself anywhere a value of the same type would be sufficient. You can also assign the return value to a variable. For example, the following two code examples accomplish the same goal:
The examples above are expression bodied members. Expression bodied members return the value returned by the expression.

You can also choose to define your methods with a statement body and a `return` statement:

:::code language="csharp" source="snippets/methods/return44.cs" id="snippet43":::

To use a value returned from a method, the calling method can use the method call itself anywhere a value of the same type would be sufficient. You can also assign the return value to a variable. For example, the following three code examples accomplish the same goal:

:::code language="csharp" source="snippets/methods/return44.cs" id="snippet45":::

:::code language="csharp" source="snippets/methods/return44.cs" id="snippet46":::

:::code language="csharp" source="snippets/methods/return44.cs" id="snippet47":::

Sometimes, you want your method to return more than a single value. You use *tuple types* and *tuple literals* to return multiple values. The tuple type defines the data types of the tuple's elements. Tuple literals provide the actual values of the returned tuple. In the following example, `(string, string, string, int)` defines the tuple type returned by the `GetPersonalInfo` method. The expression `(per.FirstName, per.MiddleName, per.LastName, per.Age)` is the tuple literal; the method returns the first, middle, and family name, along with the age, of a `PersonInfo` object.

```csharp
Expand Down
19 changes: 18 additions & 1 deletion docs/csharp/snippets/methods/return44.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
//<Snippet44>
//<Snippet43>
class SimpleMathExtnsion
{
public int DivideTwoNumbers(int number1, int number2)
{
return number1 / number2;
}
}
//</Snippet43>

//<Snippet44>
class SimpleMath
{
public int AddTwoNumbers(int number1, int number2) =>
Expand All @@ -14,6 +24,7 @@ static class TestSimpleMath
static void Main()
{
var obj = new SimpleMath();
var obj2 = new SimpleMathExtnsion();

//<Snippet45>
int result = obj.AddTwoNumbers(1, 2);
Expand All @@ -27,5 +38,11 @@ static void Main()
// The result is 9.
Console.WriteLine(result);
//</Snippet46>

//<Snippet47>
result = obj2.DivideTwoNumbers(6,2);
// The result is 3.
Console.WriteLine(result);
//</Snippet47>
}
}