Deep Cuts

WTF a deep cut?

In the days when we all got our music on vinyl, a single wasn’t something you played on Spotify or Apple Music—​you went down to the record store and bought it. The single would take up one side of the record, and the artist could put another song on the back. The A-side was the hit everyone knew from the radio, and the B-side was a lesser-known track — sometimes a real gem, like "Revolution" on the b-side of "Hey Jude." And on a full album, a deep cut is a song buried deep in an album that wasn’t a radio single but 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.

This section is like that: cool stuff that isn’t front and center in the course, 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.

Deep cuts

Things are really are useful and relevant, but just not quite critical enough to include in the chapter.

B-sides

Things that are outside the scope of the course, but that nerds or more advanced students might like.

And just to be clear, this is what normal books would call the appendices…​

Binary Numbers and How Computers Work

B-Side 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.

Example 10.1 - Outtputting variables with interpolation - alternative
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.

Example A2 - 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.

Example A3 - 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)

Recursion

B-Side to Chapter 5 - Loops

Coming Soon!