Please enable JavaScript.
Coggle requires JavaScript to display documents.
Chapter 3 Methods (Parameters (Calls (With C# we can specify which…
Chapter 3 Methods
Parameters
Optional Parameters
You specify that a parameter is optional when you define a method by providing a default value
for the parameter
void optMethod(int first, double second = 0.0, string third = “Hello”)
{
...
}
-
-
-
Calls
-
Another Example
double calculateFee(double dailyRate=500.0,int noOfDays=1){
...
}
In the call if we wanted to specify one of the arguments we could do
calculateFee(dailyRate : 200.0);
ReadLine
-
it reads user input
from the keyboard, finishing when the user presses the Enter key
The text typed
by the user is passed back as the return value. The text is returned as a string value.
General
Similar to a function
C# does not
support global methods. You must write all your methods inside a class; otherwise, your
code will not compile.
-
Syntax
-
If your method does not return anything, you can also omit the return statement because the
method finishes automatically when execution arrives at the closing brace at the end of the method.
Although this practice is common, it is not always considered good style
-
Terms
-
Class scope
You can also define variables with different
scope; for example, you can define a variable outside of a method but within a class, and this variable
can be accessed by any method within that class. Such a variable is said to have class scope.
-