Compare commits
4 Commits
9aa8bc1e4a
...
4363c2d1bf
Author | SHA1 | Date | |
---|---|---|---|
4363c2d1bf | |||
90a70ece72 | |||
61ce246ed3 | |||
af5707eea8 |
@ -271,6 +271,8 @@ randomly generated numbers):
|
||||
17 29 46 36 3
|
||||
```
|
||||
|
||||
- [Program.cs](./methods/Program.cs)
|
||||
|
||||
If your code displays different results, you'll need to review your code to
|
||||
find your error and make updates. Run the code again to see if you've fixed the
|
||||
problem. Continue updating and running your code until your code produces the
|
||||
@ -627,6 +629,8 @@ find your error and make updates. Run the code again to see if you've fixed the
|
||||
problem. Continue updating and running your code until your code produces the
|
||||
expected results.
|
||||
|
||||
- [Program.cs](./reusable_methods/Program.cs)
|
||||
|
||||
---
|
||||
|
||||
## Exercise
|
||||
@ -990,6 +994,8 @@ find your error and make updates. Run the code again to see if you've fixed the
|
||||
problem. Continue updating and running your code until your code produces the
|
||||
expected results.
|
||||
|
||||
- [Program.cs](./ipv4/Program.cs)
|
||||
|
||||
### Recap
|
||||
|
||||
Here's what you've learned about using methods so far:
|
||||
@ -1114,7 +1120,7 @@ string[] text = {
|
||||
"Today is a day to",
|
||||
"Whatever work you do",
|
||||
"This is an ideal time to"
|
||||
}
|
||||
};
|
||||
string[] good = {
|
||||
"look forward to.",
|
||||
"try new things!",
|
||||
@ -1134,9 +1140,9 @@ string[] neutral = {
|
||||
"get in tune with nature."
|
||||
};
|
||||
|
||||
TellFortune();
|
||||
tell_fortune();
|
||||
|
||||
void TellFortune() {
|
||||
void tell_fortune() {
|
||||
Console.WriteLine("A fortune teller whispers the following words:");
|
||||
string[] fortune = (luck > 75 ? good : (luck < 25 ? bad : neutral));
|
||||
for (int i = 0; i < 4; i++) {
|
||||
@ -1176,6 +1182,8 @@ check in the next unit.
|
||||
units before you continue on. All new ideas we discuss in other modules will
|
||||
depend on your understanding of the ideas that were presented in this module.
|
||||
|
||||
- [Program.cs](./fortune/Program.cs)
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
37
027_create_methods/fortune/Program.cs
Normal file
37
027_create_methods/fortune/Program.cs
Normal file
@ -0,0 +1,37 @@
|
||||
Random random = new Random();
|
||||
int luck = random.Next(100);
|
||||
|
||||
string[] text = {
|
||||
"You have much to",
|
||||
"Today is a day to",
|
||||
"Whatever work you do",
|
||||
"This is an ideal time to"
|
||||
};
|
||||
string[] good = {
|
||||
"look forward to.",
|
||||
"try new things!",
|
||||
"is likely to succeed.",
|
||||
"accomplish your dreams!"
|
||||
};
|
||||
string[] bad = {
|
||||
"fear.",
|
||||
"avoid major decisions.",
|
||||
"may have unexpected outcomes.",
|
||||
"re-evaluate your life."
|
||||
};
|
||||
string[] neutral = {
|
||||
"appreciate.",
|
||||
"enjoy time with friends.",
|
||||
"should align with your values.",
|
||||
"get in tune with nature."
|
||||
};
|
||||
|
||||
void tell_fortune() {
|
||||
Console.WriteLine("A fortune teller whispers the following words:");
|
||||
string[] fortune = (luck > 75 ? good : (luck < 25 ? bad : neutral));
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Console.WriteLine($"{text[i]} {fortune[i]} ");
|
||||
}
|
||||
}
|
||||
|
||||
tell_fortune();
|
10
027_create_methods/fortune/fortune.csproj
Normal file
10
027_create_methods/fortune/fortune.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
61
027_create_methods/ipv4/Program.cs
Normal file
61
027_create_methods/ipv4/Program.cs
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
if ipAddress consists of 4 numbers
|
||||
and
|
||||
if each ipAddress number has no leading zeroes
|
||||
and
|
||||
if each ipAddress number is in range 0 - 255
|
||||
|
||||
then ipAddress is valid
|
||||
|
||||
else ipAddress is invalid
|
||||
*/
|
||||
|
||||
string ip_addr_1 = "94.88.1.16";
|
||||
string ip_addr_2 = "255.0.0.255";
|
||||
string ip_addr_3 = "555..0.555";
|
||||
string ip_addr_4 = "255.015.0.255";
|
||||
string ip_addr_5 = "16.f.0.16";
|
||||
string[] ip_addrs = {ip_addr_1, ip_addr_2, ip_addr_3, ip_addr_4, ip_addr_5};
|
||||
|
||||
bool validate_ip(string ipv4) {
|
||||
bool valid = false;
|
||||
bool lenght = false;
|
||||
bool zeroes = false;
|
||||
bool range = false;
|
||||
string[] ip_segs = ipv4.Split('.');
|
||||
foreach (string segm in ip_segs){
|
||||
lenght = segm.Length>0 && segm.Length<=3;
|
||||
if(lenght){
|
||||
Int16 segment;
|
||||
valid = Int16.TryParse(segm, out segment);
|
||||
if(!valid){
|
||||
Console.Write($"({segm}) is a invalid ipv4 segment! ");
|
||||
return false;
|
||||
} else {
|
||||
if(segm.StartsWith("0") && segm.Length > 1 || segment < 0 || segment > 255){
|
||||
Console.Write($"({segm}) is a invalid ipv4 range! ");
|
||||
return false;
|
||||
} else {
|
||||
range = true;
|
||||
zeroes = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Console.Write($"({segm}) is a invalid ipv4 lenght! ");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(valid && zeroes && lenght && range) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (string ip_addr in ip_addrs){
|
||||
if(validate_ip(ip_addr)){
|
||||
Console.WriteLine($"{ip_addr} is a valid IP");
|
||||
} else {
|
||||
Console.WriteLine($"{ip_addr} is not a valid IP");
|
||||
}
|
||||
}
|
10
027_create_methods/ipv4/ipv4.csproj
Normal file
10
027_create_methods/ipv4/ipv4.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
33
027_create_methods/methods/Program.cs
Normal file
33
027_create_methods/methods/Program.cs
Normal file
@ -0,0 +1,33 @@
|
||||
Console.WriteLine("Before calling a method");
|
||||
SayHello();
|
||||
Console.WriteLine("After calling a method");
|
||||
|
||||
void SayHello() {
|
||||
Console.WriteLine("Hello World!");
|
||||
}
|
||||
|
||||
void DisplayRandomNumbers01() {
|
||||
Random random = new Random();
|
||||
}
|
||||
|
||||
void DisplayRandomNumbers02() {
|
||||
Random random = new Random();
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
Console.Write($"{random.Next(1, 100)} ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
void DisplayRandomNumbers03() {
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
Console.Write($"{random.Next(1, 100)} ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("\nGenerating random numbers:");
|
||||
DisplayRandomNumbers01();
|
||||
DisplayRandomNumbers02();
|
||||
DisplayRandomNumbers03();
|
10
027_create_methods/methods/methods.csproj
Normal file
10
027_create_methods/methods/methods.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
51
027_create_methods/reusable_methods/Program.cs
Normal file
51
027_create_methods/reusable_methods/Program.cs
Normal file
@ -0,0 +1,51 @@
|
||||
//using System;
|
||||
int[] times = {800, 1200, 1600, 2000};
|
||||
int diff = 0;
|
||||
|
||||
/* Format and display medicine times */
|
||||
void display_times(int[] times){
|
||||
foreach (int val in times) {
|
||||
string time = val.ToString();
|
||||
int len = time.Length;
|
||||
if (len >= 3) {
|
||||
time = time.Insert(len - 2, ":");
|
||||
}
|
||||
else if (len == 2) {
|
||||
time = time.Insert(0, "0:");
|
||||
} else {
|
||||
time = time.Insert(0, "0:0");
|
||||
}
|
||||
Console.Write($"{time} ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
void adjust_times(int diff){
|
||||
// Adjust the times by adding the difference,
|
||||
// keeping the value within 24 hours
|
||||
for (int i = 0; i < times.Length; i++) {
|
||||
times[i] = ((times[i] + diff)) % 2400;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Enter current GMT");
|
||||
int current_GMT = Convert.ToInt32(Console.ReadLine());
|
||||
|
||||
Console.WriteLine("Current Medicine Schedule:");
|
||||
display_times(times);
|
||||
|
||||
Console.WriteLine("Enter new GMT");
|
||||
int new_GMT = Convert.ToInt32(Console.ReadLine());
|
||||
|
||||
if (Math.Abs(new_GMT) > 12 || Math.Abs(current_GMT) > 12) {
|
||||
Console.WriteLine("Invalid GMT");
|
||||
} else if (new_GMT <= 0 && current_GMT <= 0 || new_GMT >= 0 && current_GMT >= 0) {
|
||||
diff = 100 * (Math.Abs(new_GMT) - Math.Abs(current_GMT));
|
||||
adjust_times(diff);
|
||||
} else {
|
||||
diff = 100 * (Math.Abs(new_GMT) + Math.Abs(current_GMT));
|
||||
adjust_times(diff);
|
||||
}
|
||||
|
||||
Console.WriteLine("New Medicine Schedule:");
|
||||
display_times(times);
|
10
027_create_methods/reusable_methods/reusable_methods.csproj
Normal file
10
027_create_methods/reusable_methods/reusable_methods.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
899
028_method_parameters/028_csharp.md
Normal file
899
028_method_parameters/028_csharp.md
Normal file
@ -0,0 +1,899 @@
|
||||
# Create C# methods with parameters
|
||||
|
||||
Learn how to use different types of input parameters in methods
|
||||
|
||||
### Learning objectives
|
||||
|
||||
- Learn more about using parameters
|
||||
|
||||
- Understand method scope
|
||||
|
||||
- Understand pass-by-reference and pass-by-value parameter types
|
||||
|
||||
- Learn how to use optional and named arguments
|
||||
|
||||
## Introduction
|
||||
|
||||
Methods have the ability to perform operations on input. Passing parameters to
|
||||
your methods allows you to perform the method's task with different input
|
||||
values. Using method parameters lets you extend your code while keeping your
|
||||
program organized and readable. If you consider a method to be a black box that
|
||||
accepts input and performs a single task, you can quickly divide a large
|
||||
problem into workable pieces.
|
||||
|
||||
Suppose you need to write code that performs the same operation on different
|
||||
sets of input. You might have three different arrays, and need to display the
|
||||
contents of each one. You can create a `DisplayArray` method that accepts a
|
||||
single array as input and displays the contents. Instead of writing code to
|
||||
display each individual array, you can call the same method and provide the
|
||||
different arrays as input.
|
||||
|
||||
Parameters can make your methods more robust while still performing the same
|
||||
general task. In this module, you'll learn more about working with parameters
|
||||
and solidify your understanding of methods.
|
||||
|
||||
### Learning Objectives
|
||||
|
||||
In this module, you will:
|
||||
|
||||
- Learn more about using parameters
|
||||
- Understand method scope
|
||||
- Understand pass-by-reference and pass-by-value parameter types
|
||||
- Learn how to use optional and named arguments
|
||||
|
||||
#### Prerequisites
|
||||
|
||||
- Experience using C# data types including `int`, `string`, arrays, and 2D
|
||||
arrays
|
||||
- Experience using switch statements, if-else statements, and for-loops
|
||||
- Experience using the `Random` class to generate a random number.
|
||||
- Basic understanding of C# methods
|
||||
|
||||
---
|
||||
|
||||
## Exercise
|
||||
|
||||
### Use parameters in methods
|
||||
|
||||
When creating methods, you'll often want to provide some information for the
|
||||
method to use. Information consumed by a method is called a parameter. You can
|
||||
supply as many parameters as needed to accomplish its task, or none at all.
|
||||
|
||||
The terms 'parameter' and 'argument' are often used interchangeably. However,
|
||||
'parameter' refers to the variable in the method signature. The 'argument' is
|
||||
the value passed when the method is called.
|
||||
|
||||
### Add parameters to methods
|
||||
|
||||
Parameters in a method work similar to variables. A parameter is defined by
|
||||
specifying the data type followed by the name of the parameter. Parameters are
|
||||
declared in the method signature, and the values for the parameters are
|
||||
provided by the method caller instead of being initialized inside the method
|
||||
itself. Consider the following code:
|
||||
|
||||
```cs
|
||||
CountTo(5);
|
||||
|
||||
void CountTo(int max) {
|
||||
for (int i = 0; i < max; i++) {
|
||||
Console.Write($"{i}, ");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this example, the method `CountTo` accepts an integer parameter named `max`.
|
||||
The parameter is referenced in the `for` loop of the method. When `CountTo` is
|
||||
called, the integer 5 is supplied as an argument.
|
||||
|
||||
In this exercise, you'll learn how to create and use your own method parameters.
|
||||
|
||||
#### Create a method with parameters
|
||||
|
||||
In this task, you'll create a method that adjusts scheduled times to a different
|
||||
GMT time zone. The method should accept a list of times, the current time zone,
|
||||
and the new time zone. Let's get started!
|
||||
|
||||
Enter the following code into the code editor:
|
||||
|
||||
```cs
|
||||
int[] schedule = {800, 1200, 1600, 2000};
|
||||
```
|
||||
|
||||
To create a method with parameters, enter the following code on a new blank
|
||||
line:
|
||||
|
||||
```cs
|
||||
void DisplayAdjustedTimes(int[] times, int currentGMT, int newGMT) {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Notice that parameters are declared similar to the way you declare variables,
|
||||
using the data type followed by the variable name. You can use parameters of
|
||||
any data type, such as `string`, `bool`, `int`, `arrays`, and more! Multiple
|
||||
parameters in a method are always comma separated.
|
||||
|
||||
Enter the following code in the `DisplayAdjustedTimes` method:
|
||||
|
||||
```cs
|
||||
int diff = 0;
|
||||
if (Math.Abs(newGMT) > 12 || Math.Abs(currentGMT) > 12) {
|
||||
Console.WriteLine("Invalid GMT");
|
||||
}
|
||||
```
|
||||
|
||||
Notice how you don't have to declare the variables `newGMT` and `currentGMT`
|
||||
since they're already declared in the method signature. You also don't
|
||||
initialize the variables since the method assumes the caller supplies those
|
||||
arguments with assigned values.
|
||||
|
||||
In this step, you create `int diff` to store the time difference and then check
|
||||
to see that the provided GMT values are between -12 and 12. Using `Math.Abs`
|
||||
gives you the absolute value of a number, so the GMT values are invalid if
|
||||
they're greater than 12.
|
||||
|
||||
To calculate the time difference, update the `DisplayAdjustedTimes` method as
|
||||
follows:
|
||||
|
||||
```cs
|
||||
int diff = 0;
|
||||
if (Math.Abs(newGMT) > 12 || Math.Abs(currentGMT) > 12) {
|
||||
Console.WriteLine("Invalid GMT");
|
||||
} else if (newGMT <= 0 && currentGMT <= 0 || newGMT >= 0 && currentGMT >= 0) {
|
||||
diff = 100 * (Math.Abs(newGMT) - Math.Abs(currentGMT));
|
||||
} else {
|
||||
diff = 100 * (Math.Abs(newGMT) + Math.Abs(currentGMT));
|
||||
}
|
||||
```
|
||||
|
||||
In this code, you check to see whether you need to add or subtract the absolute
|
||||
values of the GMT time zones to get the difference in hours. If the GMT values
|
||||
share the same sign (both positive or both negative), then the hours difference
|
||||
is equal to the difference between the two numbers. If the GMT values have
|
||||
opposite signs, then the difference is equal to the sum of the two numbers.
|
||||
Since hours are represented in hundreds, you multiply the result by 100.
|
||||
|
||||
To display the results, enter the following code at the end of the
|
||||
`DisplayAdjustedTimes` method:
|
||||
|
||||
```cs
|
||||
for (int i = 0; i < times.Length; i++) {
|
||||
int newTime = ((times[i] + diff)) % 2400;
|
||||
Console.WriteLine($"{times[i]} -> {newTime}");
|
||||
}
|
||||
```
|
||||
|
||||
To call your method, enter the following code after the `int[]` schedule
|
||||
variable declaration:
|
||||
|
||||
```cs
|
||||
DisplayAdjustedTimes(schedule, 6, -6);
|
||||
```
|
||||
|
||||
Notice that both variables and literals can be supplied as arguments to a
|
||||
method. By using input parameters, the method isn't restricted to using the
|
||||
values of global variables.
|
||||
|
||||
### Check Your Work
|
||||
|
||||
At the Terminal command prompt, enter `dotnet run`
|
||||
|
||||
Verify that your code produces the following output:
|
||||
|
||||
```txt
|
||||
800 -> 2000
|
||||
1200 -> 0
|
||||
1600 -> 400
|
||||
2000 -> 800
|
||||
```
|
||||
|
||||
If your code displays different results, you'll need to review your code to
|
||||
find your error and make updates. Run the code again to see if you've fixed the
|
||||
problem. Continue updating and running your code until your code produces the expected results.
|
||||
|
||||
- [Program.cs](./parameters/Program.cs)
|
||||
|
||||
### Recap
|
||||
|
||||
Here's what you've learned about parameters so far:
|
||||
|
||||
- Information can be passed to methods in the form of parameters.
|
||||
- Parameters are declared in the method signature.
|
||||
- Multiple parameters are separated by commas.
|
||||
- Methods can accept variable or literal arguments.
|
||||
|
||||
---
|
||||
|
||||
## Exercise
|
||||
|
||||
### Understand method scope
|
||||
|
||||
`for` loops, `if-else` statements, and methods all represent different types of
|
||||
code blocks. Each code block has its own 'scope'. 'Scope' is the region of a
|
||||
program where certain data is accessible. Variables declared inside a method,
|
||||
or any code block, are only accessible within that region. As programs become
|
||||
more complicated, this pattern helps programmers consistently use clearly named
|
||||
variables and maintain easy to read code.
|
||||
|
||||
In this exercise, you'll learn more about method scope by working with
|
||||
different types of methods and variables.
|
||||
|
||||
#### Test variable scope
|
||||
|
||||
Statements declared outside of any code block are called top-level statements.
|
||||
Variables declared in top-level statements are called 'global variables'. Global
|
||||
variables aren't restricted to any scope, and can be used anywhere throughout
|
||||
the program. Global variables can be useful for different methods that need to
|
||||
access the same data. However, it's important to pay attention to variable
|
||||
names in different scopes.
|
||||
|
||||
Enter the following code into the code editor:
|
||||
|
||||
```cs
|
||||
string[] students = {"Jenna", "Ayesha", "Carlos", "Viktor"};
|
||||
|
||||
DisplayStudents(students);
|
||||
DisplayStudents(new string[] {"Robert","Vanya"});
|
||||
|
||||
void DisplayStudents(string[] students) {
|
||||
foreach (string student in students) {
|
||||
Console.Write($"{student}, ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
```
|
||||
|
||||
In this code, you create a global `students` array, and a method
|
||||
`DisplayStudents` that accepts a parameter with the same name.
|
||||
|
||||
Save and run the code to observe the following output:
|
||||
|
||||
```txt
|
||||
Jenna, Ayesha, Carlos, Viktor,
|
||||
Robert, Vanya,
|
||||
```
|
||||
|
||||
Notice that the method parameter `student` takes precedence over the global
|
||||
`student` array. It's important to be deliberate about what global variables you
|
||||
want your methods to use.
|
||||
|
||||
Delete the previous code.
|
||||
|
||||
Enter the following code into the Editor:
|
||||
|
||||
```cs
|
||||
PrintCircleArea(12);
|
||||
|
||||
void PrintCircleArea(int radius) {
|
||||
double pi = 3.14159;
|
||||
double area = pi * (radius * radius);
|
||||
Console.WriteLine($"Area = {area}");
|
||||
}
|
||||
```
|
||||
|
||||
This code calculates and displays the area of a circle.
|
||||
|
||||
Attempt to reference the variables inside of the `PrintCircleArea` method by
|
||||
updating your code as follows:
|
||||
|
||||
```cs
|
||||
PrintCircleArea(12);
|
||||
double circumference = 2 * pi * radius;
|
||||
```
|
||||
|
||||
Error messages appear informing you that the names `pi` and `radius` don't exist
|
||||
in the current scope. Those variables only exist within the `PrintCircleArea`
|
||||
method scope.
|
||||
|
||||
Delete the incorrect code and add the following code:
|
||||
|
||||
```cs
|
||||
void PrintCircleCircumference(int radius) {
|
||||
double pi = 3.14159;
|
||||
double circumference = 2 * pi * radius;
|
||||
Console.WriteLine($"Circumference = {circumference}");
|
||||
}
|
||||
```
|
||||
|
||||
Since the variable `pi` is set to the same fixed value and used in both methods,
|
||||
this value is a good candidate for a global variable. In this example, `radius`
|
||||
isn't a global variable so that you can call the methods with different values
|
||||
of `radius` without updating a variable each time.
|
||||
|
||||
Update your code to the following:
|
||||
|
||||
```cs
|
||||
double pi = 3.14159;
|
||||
|
||||
void PrintCircleArea(int radius) {
|
||||
double area = pi * (radius * radius);
|
||||
Console.WriteLine($"Area = {area}");
|
||||
}
|
||||
|
||||
void PrintCircleCircumference(int radius) {
|
||||
double circumference = 2 * pi * radius;
|
||||
Console.WriteLine($"Circumference = {circumference}");
|
||||
}
|
||||
```
|
||||
|
||||
Now both methods can reference the same value of `pi` without needing to define
|
||||
it. You might have already guessed that methods can call other methods.
|
||||
Generally, as long as a method is defined within the scope of your program, it
|
||||
can be called anywhere.
|
||||
|
||||
Add a new method to your code as follows:
|
||||
|
||||
```cs
|
||||
double pi = 3.14159;
|
||||
PrintCircleInfo(12);
|
||||
PrintCircleInfo(24);
|
||||
|
||||
void PrintCircleInfo(int radius) {
|
||||
Console.WriteLine($"Circle with radius {radius}");
|
||||
PrintCircleArea(radius);
|
||||
PrintCircleCircumference(radius);
|
||||
}
|
||||
```
|
||||
|
||||
In this code, you create a new method `PrintCircleInfo` to call the existing
|
||||
methods. The value of `radius` is also passed down to each method. Creating
|
||||
modularized methods can help keep your code organized and easy to read.
|
||||
|
||||
Save and run the code to observe the following output:
|
||||
|
||||
```txt
|
||||
Circle with radius 12
|
||||
Area = 452.38896
|
||||
Circumference = 75.39815999999999
|
||||
Circle with radius 24
|
||||
Area = 1809.55584
|
||||
Circumference = 150.79631999999998
|
||||
```
|
||||
|
||||
- [Program.cs](./parameter_scope/Program.cs)
|
||||
|
||||
### Recap
|
||||
|
||||
Here's what you've learned about method scope so far:
|
||||
|
||||
Variables declared inside of a method are only accessible to that method.
|
||||
Variables declared in top-level statements are accessible throughout the program.
|
||||
Methods don't have access to variables defined within different methods.
|
||||
Methods can call other methods.
|
||||
|
||||
---
|
||||
|
||||
## Exercise
|
||||
|
||||
### Use value and reference type parameters
|
||||
|
||||
In C#, variables can be categorized into two main types, value types and
|
||||
reference types. These types describe how variables store their values.
|
||||
|
||||
Value types such as `int`, `bool`, `float`, `double`, and `char` directly
|
||||
contain values. Reference types such as `string`, `array`, and objects (such as
|
||||
instances of `Random`) don't store their values directly. Instead, reference
|
||||
types store an address where their value is being stored.
|
||||
|
||||
### Parameters passed by value and passed by reference
|
||||
|
||||
When an argument is passed to a method, *value* type variables have their
|
||||
values copied into the method. Each variable has its own copy of the value, so
|
||||
the original variable isn't modified.
|
||||
|
||||
With reference types, the address of the value is passed into the method. The
|
||||
variable given to the method references the value at that address, so
|
||||
operations on that variable affect the value that is referenced by the other.
|
||||
|
||||
> Note
|
||||
> It is important to remember that `string` is a reference type, but it is
|
||||
immutable. That means once it has been assigned a value, it can't be altered.
|
||||
In C#, when methods and operators are used to modify a string, the result that
|
||||
is returned is actually a new string object.
|
||||
|
||||
In this exercise, you'll learn more about passing reference and value type
|
||||
arguments into methods.
|
||||
|
||||
#### Test pass by value
|
||||
|
||||
Enter the following code into the code editor:
|
||||
|
||||
```cs
|
||||
int a = 3;
|
||||
int b = 4;
|
||||
int c = 0;
|
||||
|
||||
Multiply(a, b, c);
|
||||
Console.WriteLine($"global statement: {a} x {b} = {c}");
|
||||
|
||||
void Multiply(int a, int b, int c) {
|
||||
c = a * b;
|
||||
Console.WriteLine($"inside Multiply method: {a} x {b} = {c}");
|
||||
}
|
||||
```
|
||||
|
||||
The variables `a`, `b`, and `c` are passed to the `Multiply` method. The values
|
||||
of the variables are printed during the method execution, and printed again
|
||||
after the method is complete.
|
||||
|
||||
Integers are value types, which have their values copied when passed into
|
||||
methods. What do you think the output of `c` will be?
|
||||
|
||||
Save and run your code to observe the following output:
|
||||
|
||||
```cs
|
||||
inside Multiply method: 3 x 4 = 12
|
||||
global statement: 3 x 4 = 0
|
||||
```
|
||||
|
||||
Notice that the value of `c` is only altered within the `Multiply` method.
|
||||
Outside of the method, `c` retains its original value.
|
||||
|
||||
#### Test pass by reference
|
||||
|
||||
Enter the following code into the code editor:
|
||||
|
||||
```cs
|
||||
int[] array = {1, 2, 3, 4, 5};
|
||||
|
||||
PrintArray(array);
|
||||
Clear(array);
|
||||
PrintArray(array);
|
||||
|
||||
void PrintArray(int[] array) {
|
||||
foreach (int a in array) {
|
||||
Console.Write($"{a} ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
void Clear(int[] array) {
|
||||
for (int i = 0; i < array.Length; i++) {
|
||||
array[i] = 0;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The code begins with `array` initialized to contain some integer values. The
|
||||
values are displayed using the `PrintArray` method. The `Clear` method is called
|
||||
on the array, and then the array is printed again.
|
||||
|
||||
Arrays are reference types. Reference types store the address of their values
|
||||
in memory. What do you think the output will be?
|
||||
|
||||
Save and run your code to observe the following output:
|
||||
|
||||
```txt
|
||||
1 2 3 4 5
|
||||
0 0 0 0 0
|
||||
```
|
||||
|
||||
Notice that the array remains altered outside of the `Clear` method scope. This
|
||||
happens because the `Clear` method updated the values stored at each address.
|
||||
|
||||
#### Test with strings
|
||||
|
||||
Earlier, you learned that strings are an *immutable* type. Even though a string
|
||||
is a reference type, unlike an array, its value can't be altered once it's
|
||||
assigned. You might have noticed this if you've used methods such as
|
||||
`string.Replace` or `string.ToUpper`. In this task, you'll learn to correct a
|
||||
common error found when working with strings.
|
||||
|
||||
Enter the following code into the code editor:
|
||||
|
||||
```cs
|
||||
string status = "Healthy";
|
||||
|
||||
Console.WriteLine($"Start: {status}");
|
||||
SetHealth(status, false);
|
||||
Console.WriteLine($"End: {status}");
|
||||
|
||||
void SetHealth(string status, bool isHealthy) {
|
||||
status = (isHealthy ? "Healthy" : "Unhealthy");
|
||||
Console.WriteLine($"Middle: {status}");
|
||||
}
|
||||
```
|
||||
|
||||
Save and run your code to observe the following output:
|
||||
|
||||
```txt
|
||||
Start: Healthy
|
||||
Middle: Unhealthy
|
||||
End: Healthy
|
||||
```
|
||||
|
||||
If the `SetHealth` method didn't output the status, you might have assumed the
|
||||
method didn't execute correctly. Instead, a new string with the value
|
||||
"Unhealthy" was created and then lost in the method scope.
|
||||
|
||||
To correct this problem, you can change `SetHealth` to use the global status
|
||||
variable instead.
|
||||
|
||||
Update your code as follows:
|
||||
|
||||
```cs
|
||||
string status = "Healthy";
|
||||
|
||||
Console.WriteLine($"Start: {status}");
|
||||
SetHealth(false);
|
||||
Console.WriteLine($"End: {status}");
|
||||
|
||||
void SetHealth(bool isHealthy) {
|
||||
status = (isHealthy ? "Healthy" : "Unhealthy");
|
||||
Console.WriteLine($"Middle: {status}");
|
||||
}
|
||||
```
|
||||
|
||||
In this code, you overwrite the global `status` variable with the new string
|
||||
value.
|
||||
|
||||
Save and run your code to observe the following output:
|
||||
|
||||
```txt
|
||||
Start: Healthy
|
||||
Middle: Unhealthy
|
||||
End: Unhealthy
|
||||
```
|
||||
|
||||
Now the updated string is captured and stored correctly.
|
||||
|
||||
- [Program.cs](./value_and_ref_params/Program.cs)
|
||||
|
||||
### Recap
|
||||
|
||||
Here's what you've learned about value type and reference type parameters so
|
||||
far:
|
||||
|
||||
- Variables can be categorized as value types and reference types.
|
||||
- Value types directly contain values, and reference types store the address of
|
||||
the value.
|
||||
- Methods using value type arguments create their own copy of the values.
|
||||
- Methods that perform changes on an array parameter affect the original input
|
||||
array.
|
||||
- String is an immutable reference type.
|
||||
- Methods that perform changes on a string parameter don't affect the original
|
||||
string.
|
||||
|
||||
---
|
||||
|
||||
## Exercise
|
||||
|
||||
### Methods with optional parameters
|
||||
|
||||
The C Sharp language allows the use of named and optional parameters. These
|
||||
types of parameters let you select which arguments you want to supply to the
|
||||
method, so you aren't restricted to the structure defined in the method
|
||||
signature.
|
||||
|
||||
Named arguments allow you to specify the value for a parameter using its name
|
||||
rather than position. Optional parameters allow you to omit those arguments
|
||||
when calling the method.
|
||||
|
||||
In this exercise, you'll learn how to use both named and optional parameters.
|
||||
|
||||
#### Create an RSVP application
|
||||
|
||||
In this task, you'll create a brief application for guests to RSVP to an event.
|
||||
The guests will provide their party size and any allergies. You'll also add the
|
||||
option to restrict RSVPs to an invite-only guest list.
|
||||
|
||||
Type the following code into the Visual Studio Code Editor:
|
||||
|
||||
```cs
|
||||
string[] guestList = {"Rebecca", "Nadia", "Noor", "Jonte"};
|
||||
string[] rsvps = new string[10];
|
||||
int count = 0;
|
||||
|
||||
void RSVP(string name, int partySize, string allergies, bool inviteOnly) {
|
||||
if (inviteOnly) {
|
||||
// search guestList before adding rsvp
|
||||
}
|
||||
rsvps[count] = $"Name: {name}, \tParty Size: {partySize}, \tAllergies: {allergies}";
|
||||
count++;
|
||||
}
|
||||
|
||||
void ShowRSVPs() {
|
||||
Console.WriteLine("\nTotal RSVPs:");
|
||||
for (int i = 0; i < count; i++){
|
||||
Console.WriteLine(rsvps[i]);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this code, you create variables to store the guest list and rsvps. The `RSVP`
|
||||
method appends guest information to the list, and the `ShowRSVPs` method
|
||||
displays the total RSVPs using the tab escape sequence to separate guest
|
||||
information.
|
||||
|
||||
Enter the following code in the `RSVP` method to search the guest list:
|
||||
|
||||
```cs
|
||||
if (inviteOnly) {
|
||||
bool found = false;
|
||||
foreach (string guest in guestList) {
|
||||
if (guest.Equals(name)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
Console.WriteLine($"Sorry, {name} is not on the guest list");
|
||||
return;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this code, you check to see if the given name is equal to any of the names
|
||||
on the guest list. If a match is found, you set `found` to true and break out
|
||||
of the `foreach` loop. If `found` is false, you display a message and use the
|
||||
`return` keyword to terminate the method.
|
||||
|
||||
Call your method by adding the following code above the `RSVP` method signature:
|
||||
|
||||
```cs
|
||||
RSVP("Rebecca", 1, "none", true);
|
||||
RSVP("Nadia", 2, "Nuts", true);
|
||||
RSVP("Linh", 2, "none", false);
|
||||
RSVP("Tony", 1, "Jackfruit", true);
|
||||
RSVP("Noor", 4, "none", false);
|
||||
RSVP("Jonte", 2, "Stone fruit", false);
|
||||
ShowRSVPs();
|
||||
```
|
||||
|
||||
Save and run the code to observe the following output:
|
||||
|
||||
```txt
|
||||
Sorry, Tony is not on the guest list
|
||||
|
||||
Total RSVPs:
|
||||
Name: Rebecca, Party Size: 1, Allergies: none
|
||||
Name: Nadia, Party Size: 2, Allergies: Nuts
|
||||
Name: Linh, Party Size: 2, Allergies: none
|
||||
Name: Noor, Party Size: 4, Allergies: none
|
||||
Name: Jonte, Party Size: 2, Allergies: Stone fruit
|
||||
```
|
||||
|
||||
#### Use named arguments
|
||||
|
||||
When calling a method that accepts many parameters, it can be tricky to
|
||||
understand what the arguments represent. Using named arguments can improve the
|
||||
readability of your code. Use a named argument by specifying the parameter name
|
||||
followed by the argument value. In this task, you'll practice using named
|
||||
arguments.
|
||||
|
||||
Locate the following line of code: `RSVP("Linh", 2, "none", false);`
|
||||
|
||||
Update the method call as follows:
|
||||
|
||||
```cs
|
||||
RSVP(
|
||||
name: "Linh",
|
||||
partySize: 2,
|
||||
allergies: "none",
|
||||
inviteOnly: false
|
||||
);
|
||||
```
|
||||
|
||||
Notice that you supply the name of the parameter, followed by a colon and the
|
||||
value. This syntax defines a named argument. It isn't necessary to name all of
|
||||
the arguments. For example, the following syntax is also valid:
|
||||
|
||||
- `RSVP("Linh", 2, allergies: "none", inviteOnly: false);`
|
||||
- `RSVP("Linh", partySize: 2, "none", false);`
|
||||
|
||||
Named arguments, when used with positional arguments, are valid if they're used
|
||||
in the correct position. Named arguments are also valid as long as they're not
|
||||
followed by any positional arguments. For example, including `"Linh"` and `2` at
|
||||
the end would be invalid:
|
||||
|
||||
`RSVP(allergies: "none", inviteOnly: false, "Linh", 2);`
|
||||
|
||||
If you entered this code, you would get the following error:
|
||||
`Named argument 'allergies' is used out-of-position but is followed by an
|
||||
unnamed argument`
|
||||
|
||||
Locate the following line of code: `RSVP("Tony", 1, "Jackfruit", true);`
|
||||
|
||||
Update the method call as follows:
|
||||
|
||||
```cs
|
||||
RSVP(
|
||||
"Tony",
|
||||
inviteOnly: true,
|
||||
allergies: "Jackfruit",
|
||||
partySize: 1
|
||||
);
|
||||
```
|
||||
|
||||
Notice that the named arguments don't have to appear in the original order.
|
||||
However, the unnamed argument `Tony` is a positional argument, and must appear
|
||||
in the matching position.
|
||||
|
||||
Save and run the code to observe the following output:
|
||||
|
||||
```txt
|
||||
Sorry, Tony is not on the guest list
|
||||
|
||||
Total RSVPs:
|
||||
Name: Rebecca, Party Size: 1, Allergies: none
|
||||
Name: Nadia, Party Size: 2, Allergies: Nuts
|
||||
Name: Linh, Party Size: 2, Allergies: none
|
||||
Name: Noor, Party Size: 4, Allergies: none
|
||||
Name: Jonte, Party Size: 2, Allergies: Stone fruit
|
||||
```
|
||||
|
||||
Notice that using named arguments doesn't change the output.
|
||||
|
||||
Declare optional parameters
|
||||
A parameter becomes optional when it's assigned a default value. If an optional parameter is omitted from the arguments, the default value is used when the method executes. In this step, you'll make the parameters partySize, allergies and inviteOnly optional.
|
||||
|
||||
To define optional parameters, update the RSVP method signature as follows:
|
||||
|
||||
```cs
|
||||
void RSVP(
|
||||
string name,
|
||||
int partySize = 1,
|
||||
string allergies = "none",
|
||||
bool inviteOnly = true
|
||||
)
|
||||
```
|
||||
|
||||
Take a moment to observe the syntax. The parameters are still separated by
|
||||
commas, but the parameters `partySize`, `allergies`, and `inviteOnly` are each
|
||||
assigned to a value.
|
||||
|
||||
Next, you'll update the calls to `RSVP` to apply the optional parameters.
|
||||
|
||||
Update your code to the following:
|
||||
|
||||
```cs
|
||||
RSVP("Rebecca");
|
||||
RSVP("Nadia", 2, "Nuts");
|
||||
RSVP(name: "Linh", partySize: 2, inviteOnly: false);
|
||||
RSVP("Tony", allergies: "Jackfruit", inviteOnly: true);
|
||||
RSVP("Noor", 4, inviteOnly: false);
|
||||
RSVP("Jonte", 2, "Stone fruit", false);
|
||||
```
|
||||
|
||||
In each method call, notice that the name is never omitted. When a method is
|
||||
called, all required arguments must always be included. However, any optional
|
||||
arguments can be omitted.
|
||||
|
||||
In this code, you removed the arguments `1, "none", true` from Rebecca's rsvp.
|
||||
Since these arguments match the default value, the result of Rebecca's rsvp is the same.
|
||||
|
||||
You removed the `inviteOnly` argument from Nadia's rsvp. Since the default
|
||||
value of `inviteOnly` is `true`, the result of Nadia's rsvp is the same.
|
||||
|
||||
You removed the `partySize` argument from Tony's rsvp. If Tony had an
|
||||
invitation, the default value of `partySize` would be used in the RSVP.
|
||||
|
||||
You removed the `allergies` argument from both Linh and Noor's rsvps. Their
|
||||
rsvps will display the default value of `none` for "Allergies".
|
||||
|
||||
Save and run the code to observe the following output:
|
||||
|
||||
```txt
|
||||
Sorry, Tony is not on the guest list
|
||||
|
||||
Total RSVPs:
|
||||
Name: Rebecca, Party Size: 1, Allergies: none
|
||||
Name: Nadia, Party Size: 2, Allergies: Nuts
|
||||
Name: Linh, Party Size: 2, Allergies: none
|
||||
Name: Noor, Party Size: 4, Allergies: none
|
||||
Name: Jonte, Party Size: 2, Allergies: Stone fruit
|
||||
```
|
||||
|
||||
Notice that the default values are used in place of omitted arguments, such as
|
||||
`partySize` and `allergies`.
|
||||
|
||||
- [Program.cs](./rsvp_app/Program.cs)
|
||||
|
||||
### Recap
|
||||
|
||||
Here's what you've learned about optional and named arguments so far:
|
||||
|
||||
- Parameters are made optional by setting a default value in the method
|
||||
signature.
|
||||
- Named arguments are specified with the parameter name, followed by a colon
|
||||
and the argument value.
|
||||
- When combining named and positional arguments, you must use the correct order
|
||||
of parameters.
|
||||
|
||||
---
|
||||
|
||||
## Exercise
|
||||
|
||||
### Complete the challenge to display email addresses
|
||||
|
||||
Code challenges reinforce what you've learned, and help you gain some
|
||||
confidence before continuing on.
|
||||
|
||||
The focus of this challenge is to create a method with the proper parameters,
|
||||
including an optional parameter.
|
||||
|
||||
### Display email addresses
|
||||
|
||||
Your challenge is to create a method that displays the correct email address
|
||||
for both internal and external employees. You're given lists of internal and
|
||||
external employee names. An employee's email address consists of their username
|
||||
and company domain name.
|
||||
|
||||
The username format is the first two characters of the employee first name,
|
||||
followed by their last name. For example, an employee named "Robert Bavin"
|
||||
would have the username "robavin". The domain for internal employees is
|
||||
"contoso.com".
|
||||
|
||||
In this challenge, you're given some starting code. You must decide how to
|
||||
create and call a method to display email addresses.
|
||||
|
||||
### Code challenge: Add a method to display email addresses
|
||||
|
||||
In the code you start with, there are two arrays for internal and external
|
||||
employees. Remember, the domain for internal employees is "contoso.com" and the
|
||||
username for all employees is the first two characters of their first name,
|
||||
followed by their full last name.
|
||||
|
||||
Your challenge is to create a method that will display the email address of
|
||||
internal and external employees. The method should include an optional
|
||||
parameter for the domain name of external employees.
|
||||
|
||||
Copy and paste the following code into the code editor.
|
||||
|
||||
```cs
|
||||
string[,] corporate = {
|
||||
{"Robert", "Bavin"}, {"Simon", "Bright"},
|
||||
{"Kim", "Sinclair"}, {"Aashrita", "Kamath"},
|
||||
{"Sarah", "Delucchi"}, {"Sinan", "Ali"}
|
||||
};
|
||||
|
||||
string[,] external = {
|
||||
{"Vinnie", "Ashton"}, {"Cody", "Dysart"},
|
||||
{"Shay", "Lawrence"}, {"Daren", "Valdes"}
|
||||
};
|
||||
|
||||
string externalDomain = "hayworth.com";
|
||||
|
||||
for (int i = 0; i < corporate.GetLength(0); i++) {
|
||||
// display internal email addresses
|
||||
}
|
||||
|
||||
for (int i = 0; i < external.GetLength(0); i++) {
|
||||
// display external email addresses
|
||||
}
|
||||
```
|
||||
|
||||
Update the code to use a method to display the email addresses according to the
|
||||
challenge specifications.
|
||||
|
||||
Use what you've learned about using parameters and optional arguments to
|
||||
complete the update.
|
||||
|
||||
Verify that your code produces the following output:
|
||||
|
||||
```txt
|
||||
robavin@contoso.com
|
||||
sibright@contoso.com
|
||||
kisinclair@contoso.com
|
||||
aakamath@contoso.com
|
||||
sadelucchi@contoso.com
|
||||
siali@contoso.com
|
||||
viashton@hayworth.com
|
||||
codysart@hayworth.com
|
||||
shlawrence@hayworth.com
|
||||
davaldes@hayworth.com
|
||||
```
|
||||
|
||||
Whether you get stuck and need to peek at the solution or you finish
|
||||
successfully, continue on to view a solution to this challenge.
|
||||
|
||||
- [Program.cs](./disp_mail_addr/Program.cs)
|
||||
|
||||
---
|
||||
|
||||
### Summary
|
||||
|
||||
Your goal was to learn more about using parameters in methods and to understand
|
||||
method scope. You learned about value and reference types and how data is
|
||||
affected inside of a method. You also learned how to use named and optional
|
||||
arguments to extend your method's capabilities.
|
27
028_method_parameters/disp_mail_addr/Program.cs
Normal file
27
028_method_parameters/disp_mail_addr/Program.cs
Normal file
@ -0,0 +1,27 @@
|
||||
string[,] corporate = {
|
||||
{"Robert", "Bavin"}, {"Simon", "Bright"},
|
||||
{"Kim", "Sinclair"}, {"Aashrita", "Kamath"},
|
||||
{"Sarah", "Delucchi"}, {"Sinan", "Ali"}
|
||||
};
|
||||
|
||||
string[,] external = {
|
||||
{"Vinnie", "Ashton"}, {"Cody", "Dysart"},
|
||||
{"Shay", "Lawrence"}, {"Daren", "Valdes"}
|
||||
};
|
||||
|
||||
const string external_domain = "hayworth.com";
|
||||
const string internal_domain = "contoso.com";
|
||||
|
||||
for (int i = 0; i < corporate.GetLength(0); i++) {
|
||||
display_email(corporate[i, 0], corporate[i, 1], internal_domain);
|
||||
}
|
||||
|
||||
for (int i = 0; i < external.GetLength(0); i++) {
|
||||
display_email(external[i, 0], external[i, 1], external_domain);
|
||||
}
|
||||
|
||||
void display_email(string first, string last, string domain=internal_domain) {
|
||||
string username = first.Substring(0, 2) + last;
|
||||
username = username.ToLower();
|
||||
Console.WriteLine($"{username}@{domain}");
|
||||
}
|
10
028_method_parameters/disp_mail_addr/disp_mail_addr.csproj
Normal file
10
028_method_parameters/disp_mail_addr/disp_mail_addr.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
35
028_method_parameters/parameter_scope/Program.cs
Normal file
35
028_method_parameters/parameter_scope/Program.cs
Normal file
@ -0,0 +1,35 @@
|
||||
string[] students = {"Jenna", "Ayesha", "Carlos", "Viktor"};
|
||||
|
||||
display_students(students);
|
||||
display_students(new string[] {"Robert","Vanya"});
|
||||
|
||||
void display_students(string[] students) {
|
||||
foreach (string student in students) {
|
||||
Console.Write($"{student}, ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
void print_circle_area(int radius) {
|
||||
double area = Math.PI * (radius * radius);
|
||||
Console.WriteLine($"Area = {area:F2}");
|
||||
}
|
||||
|
||||
void print_circle_circumference(int radius) {
|
||||
double circumference = 2 * Math.PI * radius;
|
||||
Console.WriteLine($"Circumference = {circumference:F2}");
|
||||
}
|
||||
|
||||
void print_circle_info(int radius){
|
||||
Console.WriteLine($"Circle with radius {radius}");
|
||||
print_circle_area(radius);
|
||||
print_circle_circumference(radius);
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
//print_circle_area(12);
|
||||
//print_circle_circumference(12);
|
||||
print_circle_info(12);
|
||||
print_circle_info(24);
|
10
028_method_parameters/parameter_scope/parameter_scope.csproj
Normal file
10
028_method_parameters/parameter_scope/parameter_scope.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
29
028_method_parameters/parameters/Program.cs
Normal file
29
028_method_parameters/parameters/Program.cs
Normal file
@ -0,0 +1,29 @@
|
||||
count_to(5);
|
||||
|
||||
void count_to(int max) {
|
||||
for (int i = 0; i < max; i++) {
|
||||
Console.Write($"{i}, ");
|
||||
}
|
||||
}
|
||||
|
||||
int[] schedule = {800, 1200, 1600, 2000};
|
||||
|
||||
void display_adjusted_times(int[] times, int current_GMT, int new_GMT) {
|
||||
int diff = 0;
|
||||
if (Math.Abs(new_GMT) > 12 || Math.Abs(current_GMT) > 12){
|
||||
Console.WriteLine("Invalid GMT");
|
||||
} else if (
|
||||
new_GMT <= 0 && current_GMT <= 0 ||
|
||||
new_GMT >= 0 && current_GMT >= 0
|
||||
){
|
||||
diff = 100 * (Math.Abs(new_GMT) - Math.Abs(current_GMT));
|
||||
} else {
|
||||
diff = 100 * (Math.Abs(new_GMT) + Math.Abs(current_GMT));
|
||||
}
|
||||
for (int i=0; i<times.Length; i++){
|
||||
int new_time = ((times[i] + diff)) % 2400;
|
||||
Console.WriteLine($"{times[i]} -> {new_time}");
|
||||
}
|
||||
}
|
||||
|
||||
display_adjusted_times(schedule, 6, -6);
|
10
028_method_parameters/parameters/parameters.csproj
Normal file
10
028_method_parameters/parameters/parameters.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
44
028_method_parameters/rsvp_app/Program.cs
Normal file
44
028_method_parameters/rsvp_app/Program.cs
Normal file
@ -0,0 +1,44 @@
|
||||
string[] guest_list = {"Rebecca", "Nadia", "Noor", "Jonte"};
|
||||
string[] rsvps = new string[10];
|
||||
int count = 0;
|
||||
|
||||
// void RSVP(string name, int party_size, string allergies, bool invite_only) {
|
||||
void RSVP(string name,
|
||||
int party_size = 1,
|
||||
string allergies = "none",
|
||||
bool invite_only = true) {
|
||||
if (invite_only) {
|
||||
bool found = false;
|
||||
foreach (string guest in guest_list) {
|
||||
if (guest.Equals(name)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
Console.WriteLine($"Sorry, {name} is not on the guest list");
|
||||
return;
|
||||
}
|
||||
}
|
||||
rsvps[count] = $"Name: {name}," +
|
||||
$"\tParty Size: {party_size}," +
|
||||
$"\tAllergies: {allergies}";
|
||||
count++;
|
||||
}
|
||||
|
||||
void show_RSVPs() {
|
||||
Console.WriteLine("\nTotal RSVPs:");
|
||||
for (int i = 0; i < count; i++){
|
||||
Console.WriteLine(rsvps[i]);
|
||||
}
|
||||
}
|
||||
|
||||
//RSVP("Rebecca", 1, "none", true);
|
||||
RSVP("Rebecca");
|
||||
RSVP("Nadia", 2, "Nuts");
|
||||
//RSVP("Linh", 2, "none", false);
|
||||
RSVP(name: "Linh", party_size: 2, invite_only: false);
|
||||
RSVP("Tony", 1, allergies: "Jackfruit", invite_only: true);
|
||||
RSVP("Noor", 4, invite_only: false);
|
||||
RSVP("Jonte", 2, "Stone fruit", false);
|
||||
show_RSVPs();
|
10
028_method_parameters/rsvp_app/rsvp_app.csproj
Normal file
10
028_method_parameters/rsvp_app/rsvp_app.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
47
028_method_parameters/value_and_ref_params/Program.cs
Normal file
47
028_method_parameters/value_and_ref_params/Program.cs
Normal file
@ -0,0 +1,47 @@
|
||||
int a = 3;
|
||||
int b = 4;
|
||||
int c = 0;
|
||||
|
||||
Multiply(a, b, c);
|
||||
Console.WriteLine($"global statement: {a} x {b} = {c}");
|
||||
|
||||
void Multiply(int a, int b, int c) {
|
||||
c = a * b;
|
||||
Console.WriteLine($"inside Multiply method: {a} x {b} = {c}");
|
||||
}
|
||||
|
||||
Console.WriteLine("--------------------------------");
|
||||
|
||||
int[] array = {1, 2, 3, 4, 5};
|
||||
|
||||
PrintArray(array);
|
||||
Clear(array);
|
||||
PrintArray(array);
|
||||
|
||||
void PrintArray(int[] array) {
|
||||
foreach (int a in array) {
|
||||
Console.Write($"{a} ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
void Clear(int[] array) {
|
||||
for (int i = 0; i < array.Length; i++) {
|
||||
array[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("--------------------------------");
|
||||
|
||||
string status = "Healthy";
|
||||
|
||||
Console.WriteLine($"Start: {status}");
|
||||
//SetHealth(status, false);
|
||||
SetHealth(false);
|
||||
Console.WriteLine($"End: {status}");
|
||||
|
||||
//void SetHealth(string status, bool isHealthy) {
|
||||
void SetHealth(bool isHealthy) {
|
||||
status = (isHealthy ? "Healthy" : "Unhealthy");
|
||||
Console.WriteLine($"Middle: {status}");
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
1250
029_return_value_method/029_csharp.md
Normal file
1250
029_return_value_method/029_csharp.md
Normal file
File diff suppressed because it is too large
Load Diff
45
029_return_value_method/challenge_game/Program.cs
Normal file
45
029_return_value_method/challenge_game/Program.cs
Normal file
@ -0,0 +1,45 @@
|
||||
Random random = new Random();
|
||||
|
||||
Console.WriteLine("Would you like to play? (Y/N)");
|
||||
if (should_play()) {
|
||||
play_game();
|
||||
}
|
||||
|
||||
string win_or_lose(int target, int roll) {
|
||||
if(roll > target){
|
||||
return "You win!";
|
||||
} else {
|
||||
return "You lose!";
|
||||
}
|
||||
}
|
||||
|
||||
bool should_play() {
|
||||
bool valid = false;
|
||||
string? resp;
|
||||
while (!valid) {
|
||||
resp = Console.ReadLine();
|
||||
if (resp != null) {
|
||||
resp = resp.Trim().ToLower();
|
||||
if(resp.Equals("y")){
|
||||
return true;
|
||||
} else if (resp.Equals("n")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void play_game() {
|
||||
var play = true;
|
||||
while (play) {
|
||||
var target = random.Next(1,6);
|
||||
var roll = random.Next(1,7);;
|
||||
Console.WriteLine($"Roll a number greater than {target} to win!");
|
||||
Console.WriteLine($"You rolled a {roll}");
|
||||
Console.WriteLine(win_or_lose(target, roll));
|
||||
Console.WriteLine("\nPlay again? (Y/N)");
|
||||
|
||||
play = should_play();
|
||||
}
|
||||
}
|
10
029_return_value_method/challenge_game/challenge_game.csproj
Normal file
10
029_return_value_method/challenge_game/challenge_game.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
37
029_return_value_method/return_array/Program.cs
Normal file
37
029_return_value_method/return_array/Program.cs
Normal file
@ -0,0 +1,37 @@
|
||||
//int[] two_coins(int[] coins, int target) {
|
||||
int[,] two_coins(int[] coins, int target) {
|
||||
int[,] result = {
|
||||
{-1,-1},{-1,-1},{-1,-1},
|
||||
{-1,-1},{-1,-1}
|
||||
};
|
||||
int count = 0;
|
||||
for (int curr = 0; curr < coins.Length; curr++) {
|
||||
for (int next = curr + 1; next < coins.Length; next++) {
|
||||
if (coins[curr] + coins[next] == target) {
|
||||
result[count, 0] = curr;
|
||||
result[count, 1] = next;
|
||||
count++;
|
||||
}
|
||||
if (count == result.GetLength(0)) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (count == 0) ? new int[0, 0] : result;
|
||||
}
|
||||
|
||||
int target = 55;
|
||||
int[] coins = new int[] { 5, 5, 50, 25, 25, 10, 5 };
|
||||
int[,] result = two_coins(coins, target);
|
||||
|
||||
if (result.Length == 0) {
|
||||
Console.WriteLine("No two coins make change");
|
||||
} else {
|
||||
Console.WriteLine("Change found at positions:");
|
||||
for (int i = 0; i < result.GetLength(0); i++) {
|
||||
if (result[i, 0] == -1) {
|
||||
break;
|
||||
}
|
||||
Console.WriteLine($"{result[i, 0]},{result[i, 1]}");
|
||||
}
|
||||
}
|
10
029_return_value_method/return_array/return_array.csproj
Normal file
10
029_return_value_method/return_array/return_array.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
32
029_return_value_method/return_bool/Program.cs
Normal file
32
029_return_value_method/return_bool/Program.cs
Normal file
@ -0,0 +1,32 @@
|
||||
string[] words = { "racecar", "talented", "deified", "tent", "tenet" };
|
||||
|
||||
Console.WriteLine("Is it a palindrome?");
|
||||
foreach (string word in words) {
|
||||
Console.WriteLine($"{word}: {is_palindrome(word)}");
|
||||
Console.WriteLine($"{word}: {check_palindrome(word)}");
|
||||
}
|
||||
|
||||
bool is_palindrome(string word) {
|
||||
int start = 0;
|
||||
int end = word.Length - 1;
|
||||
|
||||
while (start < end) {
|
||||
if (word[start] != word[end]) {
|
||||
return false;
|
||||
}
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool check_palindrome(string word) {
|
||||
char[] temp = word.ToCharArray();
|
||||
Array.Reverse(temp);
|
||||
string rev_word = string.Join("", temp);
|
||||
if (String.Equals(word, rev_word)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
10
029_return_value_method/return_bool/return_bool.csproj
Normal file
10
029_return_value_method/return_bool/return_bool.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
25
029_return_value_method/return_meth/Program.cs
Normal file
25
029_return_value_method/return_meth/Program.cs
Normal file
@ -0,0 +1,25 @@
|
||||
double total = 0;
|
||||
double min_spend = 30.00;
|
||||
|
||||
double[] items = {15.97, 3.50, 12.25, 22.99, 10.98};
|
||||
double[] discounts = {0.30, 0.00, 0.10, 0.20, 0.50};
|
||||
|
||||
for (int i = 0; i < items.Length; i++) {
|
||||
total += get_discounted_price(i);
|
||||
}
|
||||
|
||||
total -= total_meets_minimum() ? 5.00 : 0.00;
|
||||
|
||||
Console.WriteLine($"Total: ${format_decimal(total)}");
|
||||
|
||||
double get_discounted_price(int item_index) {
|
||||
return items[item_index] * (1 - discounts[item_index]);
|
||||
}
|
||||
|
||||
bool total_meets_minimum() {
|
||||
return total >= min_spend;
|
||||
}
|
||||
|
||||
string format_decimal(double input) {
|
||||
return input.ToString().Substring(0, 5);
|
||||
}
|
10
029_return_value_method/return_meth/return_meth.csproj
Normal file
10
029_return_value_method/return_meth/return_meth.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
18
029_return_value_method/return_nums/Program.cs
Normal file
18
029_return_value_method/return_nums/Program.cs
Normal file
@ -0,0 +1,18 @@
|
||||
double usd = 23.73;
|
||||
int vnd = usd_to_vnd(usd);
|
||||
|
||||
Console.WriteLine($"${usd} USD = ${vnd} VND");
|
||||
Console.WriteLine($"${vnd} VND = ${vnd_to_usd(vnd)} USD");
|
||||
|
||||
int usd_to_vnd(double usd) {
|
||||
int rate = 23500;
|
||||
return (int) (rate * usd);
|
||||
}
|
||||
|
||||
double vnd_to_usd(int vnd) {
|
||||
double rate = 23500;
|
||||
return vnd / rate;
|
||||
}
|
||||
|
||||
var temp = 100*0.5;
|
||||
Console.WriteLine(temp.GetType());
|
10
029_return_value_method/return_nums/return_nums.csproj
Normal file
10
029_return_value_method/return_nums/return_nums.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
22
029_return_value_method/return_str/Program.cs
Normal file
22
029_return_value_method/return_str/Program.cs
Normal file
@ -0,0 +1,22 @@
|
||||
string reverse_word(string word) {
|
||||
string result = "";
|
||||
for (int i=word.Length - 1; i>=0 ; i--) {
|
||||
result += word[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
string reverse_sentence(string input) {
|
||||
string result = "";
|
||||
string[] words = input.Split(" ");
|
||||
foreach (string word in words) {
|
||||
result += reverse_word(word) + " ";
|
||||
}
|
||||
return result.Trim();
|
||||
}
|
||||
|
||||
string input = "there are snakes at the zoo";
|
||||
|
||||
Console.WriteLine("Sentece : " + input);
|
||||
Console.WriteLine("Reversed: " + reverse_word(input));
|
||||
Console.WriteLine("Special : " + reverse_sentence(input));
|
10
029_return_value_method/return_str/return_str.csproj
Normal file
10
029_return_value_method/return_str/return_str.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
@ -32,3 +32,5 @@ Following
|
||||
25. [Work with variable data](./025_Work_with_variable_data/025_csharp.md)
|
||||
26. [Challenge - variable data](./026_Challenge_variable_data/026_csharp.md)
|
||||
27. [Create methods](./027_create_methods/027_csharp.md)
|
||||
28. [Methods with parameters](./028_method_parameters/028_csharp.md)
|
||||
29. [Methods that return values](./029_return_value_method/029_csharp.md)
|
||||
|
Loading…
Reference in New Issue
Block a user