end 028
This commit is contained in:
parent
61ce246ed3
commit
90a70ece72
@ -556,4 +556,344 @@ 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>
|
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>
|
Loading…
Reference in New Issue
Block a user