Deep Cuts
WTF a "deep cut"?
Some artists value the idea of an album as a unified creative work, and they put a lot of thought into which songs go on an album—and in what order. They might want a really catchy song at the start of the album, a strong start to the second side, and a big (or thought-provoking) finish.
A song is called a "cut" because a recording master was made by literally cutting a groove into a disc, and artists would cut a song in the studio. A deep cut is a song buried deep in an album that isn’t a radio single but is still great—the kind of song that becomes a fan favorite. Check out "Loving Cup", which is smack dab in the middle of Exile on Main St. and is easily overlooked among the hits all over that album.
This section is like that: cool stuff that isn’t critical enough to include in the chapter, but might turn out to be really interesting or useful. I like the C# language a lot, and there are a ton of great features that I don’t get into because I know many of my students don’t need a bunch of extra stuff thrown at them. So this is where I can put some of that stuff. They’re brief and, where appropriate, I link to a reliable source of additional information on the topic.
And just to be clear, this is what a normal books would call the appendices…
Binary Numbers and How Computers Work
Deep Cut from Chapter 1 - Computers and Coding
The chapters of this eBook are subtitled using binary to show basic examples of the numbering system: 0001 is, well, 1 in decimal. And 0100 is 4 in decimal. We won’t get into binary numbers, but it’s not very complicated and is kind of interesting if you’re a nerd.
The amazing code.org project has a playlist of great, short videos on how computers work—including binary numbers—if you want to know a little more without going too crazy: How Computers Work
string.Format()
Method
Deep Cut from Chapter 2 - Variables and Data Types
You can also place variable values in a string using the string.Format()
method.
This method accepts a string with numbered placeholders, along with the variables you want to insert—in the order they appear in the string.
1
2
3
4
5
6
string artistName = "Elvis Presley";
int birthYear = 1935;
string output = string.Format("{0} was born in {1}", artistName, birthYear);
Console.WriteLine(output);
The {0}
and {1}
in the string act as placeholders, and then the values are added on with commas after the string.
artistName
is the first variable listed, so it goes in the first placeholder; birthYear
is the second variable listed, so it goes in the second placeholder.
Beginners might find this style a little more confusing, but programmers coming to C# from some other languages might find it familiar.
And interpoliation is a relatively new addition to C# (around 2015, I believe), so you might still see older code using string.Format()
.
Optional Arguments
Deep Cut from Chapter 3 - Methods
C# allows us to specify that some arguments are optional. By default (the way we’ve defined parameters so far), a method call must provide a value for each parameter in the method. However, you can also assign a default value to a parameter; that makes it optional.
OptionalArgument.cs
. A method with an optional argument.1
2
3
public static double TotalBill(double meal, double tipPercentage = 20.0) {
return meal + (meal * tipPercentage/100);
}
When calling TotalBill
, we can provide one argument (price of the meal) or two (price of the meal and the tip percentage).
If we don’t provide tipPercentage
, the method will use 20.0
for that parameter.
double amount = TotalBill(100.0); // amount = 120.0
double amount2 = TotalBill(100.0, 18.0); // amount2 = 118.0
If you use optional parameters, they must be at the end of the parameter list. In other words, in the example above, I couldn’t have the optional tipPercentage before meal .
|
Learn more about optional parameters in the official Microsoft documentation: Optional Arguments (C# Programming Guide)
Named Arguments
Deep Cut from Chapter 3 - Methods
Another nice C# feature related methods is named arguments. Named arguments allow you to specify the name of the parameter you’re providing a value for, so you can list the arguments in any order. Simply put the name of the parameter followed by a colon and the value you want to assign to it.
It means you don’t have to remember the order of the parameters in the method as long as you remember the names.
NamedArguments.cs
. A method call using named arguments. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class NamedArguments
{
public static void Main()
{
double amount = TotalBill(tipPercentage: 18.0, tax: 1.0, meal: 100.0);
Console.WriteLine(amount);
static double TotalBill(double meal, double tax, double tipPercentage)
{
return meal + (meal * tax / 100) + (meal * tipPercentage / 100);
}
}
}
Learn more about named arguments in the official Microsoft documentation: Named Arguments (C# Programming Guide)