You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn-pr/wwl-language/create-c-sharp-methods-return-values/includes/3-exercise-create-methods-return-numbers.md
+90-24Lines changed: 90 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,29 +3,55 @@
3
3
4
4
You may often need to return numbers from methods and use the results for other tasks. In this brief exercise, you'll practice returning `int` and `double` data types, and capturing the return values.
5
5
6
-
## Create a method that returns an integer
6
+
## Create a method that returns an integer
7
+
7
8
Suppose you're visiting Vietnam and want to create a brief program that converts currency. You can assume the current exchange rate is `1 USD = 23500 VND`. In this task, you'll write a method that converts USD to VND.
8
9
9
10
1. In the Visual Studio Code Editor, delete any existing code from the previous exercises.
10
11
11
12
1. Enter the following code into the Visual Studio Code Editor:
12
-
```c# double usd = 23.73; int vnd = UsdToVnd(usd);
Thishappensbecausethecompilerattemptstocastthevaluereturnedtomatchthedatatypespecifiedinthemethodsignature. However, implicitcastingisonlyavailable when there's no data loss occurring as a result of the conversion. The return value must always match the data type specified in the method signature, so in this case, you must cast the result.
29
+
30
+
```c#
31
+
intUsdToVnd(doubleusd)
32
+
{
33
+
intrate=23500;
34
+
return (int) (rate*usd);
35
+
}
36
+
```
37
+
38
+
Ifyouomitthecastfromthereturnresult, you'll see the following error:
39
+
40
+
```output
41
+
Cannotimplicitlyconverttype'double'to'int'.
42
+
```
43
+
44
+
Thishappensbecausethecompilerattemptstocastthevaluereturnedtomatchthedatatypespecifiedinthemethodsignature. However, implicitcastingisonlyavailable when there's no data loss occurring as a result of the conversion. The return value must always match the data type specified in the method signature, so in this case, you must cast the result.
Ifyourcodedisplaysunexpectedresults, you'll need to review your code to find your error and make updates. Run the code again to see if you'vefixedtheproblem. Continueupdatingandrunningyourcodeuntilyourcodeproducestheexpectedresults.
49
+
50
+
```
51
+
$23.73USD= $557655VND
52
+
```
53
+
54
+
Ifyourcodedisplaysunexpectedresults, you'll need to review your code to find your error and make updates. Run the code again to see if you'vefixedtheproblem. Continueupdatingandrunningyourcodeuntilyourcodeproducestheexpectedresults.
29
55
30
56
### Create a method that returns a double
31
57
@@ -34,33 +60,73 @@ Next, you'll create a method to convert VND back to USD.
Inthiscase, youneed `rate` tobea `double` orelsethecompilerusesintegerdivisionandreturnatruncated `int` value. USDneedstoberepresentedbyadecimalnumber. Ifyouset `rate` toan `int` insteadof `double`, you'll notice that the compiler doesn'tpresentyouwithanyerrors. Thishappensbecausethevalueof `vnd/rate` isimplicitlycastedtothe `double` datatypespecifiedinthemethodsignature. Whencreatingmethodsthatreturnnumericvalues, it's important to consider the data types in the operations your method performs.
Ifyouset `rate` toan `int` insteadof `double`, you'll notice that the compiler doesn'tpresentyouwithanyerrors. Thishappensbecausethevalueof `vnd/rate` isimplicitlycastedtothe `double` datatypespecifiedinthemethodsignature. Whencreatingmethodsthatreturnnumericvalues, it's important to consider the data types in the operations your method performs.
0 commit comments