Skip to content

Commit 32f3dc1

Browse files
authored
Clarified examples regarding return values (#44735)
* clarified example for return values on methods * Fixed small type - SimpleMathExtension * Made a separate class for SimpleMathExtension * Swapped the order of the sentences describing the examples
1 parent b0731ab commit 32f3dc1

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

docs/csharp/methods.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,16 @@ You can also choose to define your methods with a statement body and a `return`
163163

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

166-
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:
166+
To use a value returned from a method, you can assign the return value to a variable:
167+
168+
:::code language="csharp" source="snippets/methods/return44.cs" id="snippet47":::
169+
170+
The calling method can also use the method call itself anywhere a value of the same type would be sufficient. For example, the following two code examples accomplish the same goal:
167171

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

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

172-
:::code language="csharp" source="snippets/methods/return44.cs" id="snippet47":::
173-
174176
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.
175177

176178
```csharp

docs/csharp/snippets/methods/return44.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//<Snippet43>
2-
class SimpleMathExtnsion
2+
class SimpleMathExtension
33
{
44
public int DivideTwoNumbers(int number1, int number2)
55
{
@@ -19,12 +19,25 @@ public int SquareANumber(int number) =>
1919
}
2020
//</Snippet44>
2121

22+
static class TestSimpleMathExtension
23+
{
24+
static void Main()
25+
{
26+
var obj = new SimpleMathExtension();
27+
28+
//<Snippet47>
29+
int result = obj.DivideTwoNumbers(6,2);
30+
// The result is 3.
31+
Console.WriteLine(result);
32+
//</Snippet47>
33+
}
34+
}
35+
2236
static class TestSimpleMath
2337
{
2438
static void Main()
2539
{
2640
var obj = new SimpleMath();
27-
var obj2 = new SimpleMathExtnsion();
2841

2942
//<Snippet45>
3043
int result = obj.AddTwoNumbers(1, 2);
@@ -38,11 +51,5 @@ static void Main()
3851
// The result is 9.
3952
Console.WriteLine(result);
4053
//</Snippet46>
41-
42-
//<Snippet47>
43-
result = obj2.DivideTwoNumbers(6,2);
44-
// The result is 3.
45-
Console.WriteLine(result);
46-
//</Snippet47>
4754
}
4855
}

0 commit comments

Comments
 (0)