diff --git a/docs/csharp/methods.md b/docs/csharp/methods.md index 4260f1975472f..313194768775f 100644 --- a/docs/csharp/methods.md +++ b/docs/csharp/methods.md @@ -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 diff --git a/docs/csharp/snippets/methods/return44.cs b/docs/csharp/snippets/methods/return44.cs index 5643f9a14cdaa..76b61dfaf9681 100644 --- a/docs/csharp/snippets/methods/return44.cs +++ b/docs/csharp/snippets/methods/return44.cs @@ -1,4 +1,14 @@ -// +// +class SimpleMathExtnsion +{ + public int DivideTwoNumbers(int number1, int number2) + { + return number1 / number2; + } +} +// + +// class SimpleMath { public int AddTwoNumbers(int number1, int number2) => @@ -14,6 +24,7 @@ static class TestSimpleMath static void Main() { var obj = new SimpleMath(); + var obj2 = new SimpleMathExtnsion(); // int result = obj.AddTwoNumbers(1, 2); @@ -27,5 +38,11 @@ static void Main() // The result is 9. Console.WriteLine(result); // + + // + result = obj2.DivideTwoNumbers(6,2); + // The result is 3. + Console.WriteLine(result); + // } }