To handle this, we can take advantage of escape characters. In C#, you start an escape sequence with the backslash character (‘\’). Inside of a string literal, you can use a backslash, followed by a quotation mark, to get it to recognize it as a quotation mark that belongs inside of the string, as opposed to one that indicates the end of the string:
Console.WriteLine("\"");
It is worth pointing out that even though \" is visually two characters, it is treated as only one.
C# has lots of different escape characters that you’ll find useful. For example, \t is the tab character, and \n is the newline character. You can see how this works in the following example:
Console.WriteLine("Text\non\nmore\nthan\none\nline.");
This will print out:
Text on more than one line.
So what happens if you want to print out an actual backslash character? By default, the computer will try to take \ and combine it with the next letter to represent an escape character, since it marks the beginning of an escape sequence. To print out the backslash character, you will need to use two backslashes:
Console.WriteLine("C:\\Users\\RB\\Desktop\\MyFile.txt");
This will print out:
C:\Users\RB\Desktop\MyFile.txt
If you find that all of these extra slashes are kind of annoying, you can put the ‘@’ symbol before the text (called a verbatim string literal) which tells the computer to ignore escape characters in the string.
The following line is equivalent to the previous one:
Console.WriteLine(@"C:\Users\RB\Desktop\MyFile.txt");
String Interpolation
While we’re on the subject of user input and output, there’s a new feature of C# 6.0 that is worth talking about here. That is string formatting and the new string interpolation feature.
It’s pretty common when you’re going to present something to the user to mix in some text with some code. Earlier in this chapter, we did this by concatenating the strings and code together with a
“+”:
Console.WriteLine("The cylinder's volume is: " + volume + " cubic units.");
C# 6.0 introduces a new feature called string interpolation. This feature lets you write this same thing in a much more concise and readable way:
Console.WriteLine($"The cylinder's volume is: {volume} cubic units.");
52 Chapter 8 User Input
String interpolation requires two steps. First, before the string, place a “$”. This tells the C# compiler that this isn’t just a normal string. It’s one that has code embedded within it that needs to be
evaluated. Within the string, when you want to put some code, you simply surround it with curly braces.
This translates to the same code as our first pass, but this is usually more readable, so it’s usually preferred. The curly braces can contain any valid C# expression. It’s not limited to just a single variable. That means you can do things like perform some simple math, or call a method, etc. You’ll even get Visual Studio auto-completion and syntax highlighting inside the curly braces.
There’s obviously a practical limit to how much you can stuff inside the curly braces before it
becomes unreadable. At some point, the logic gets complicated and long enough that you lose track of how it’s being formatted. At that point, it’s better to pull the logic out onto a separate line and store it in a local variable.
9
9 More Math
Here we are, back at more math. There’s so much math that computers can do—in fact, it is all computers can do. This will be our last chapter that focuses purely on math. We’ll soon move on to much cooler things.
But there are still quite a few things we need to discuss. The things we’re going to talk about in this chapter are not very closely related to each other. Each section is its own thing. This means that if
In a Nutshell
Division with integer types uses integer division. This means you won’t get fractional or decimal results, just the integer part of the result.
Casting can convert one type to another type. Many types are implicitly cast from narrow types to wider types. Wider types can be explicitly cast to certain narrower types using the type you want in parentheses in front: float a = (float)3.4445;
Division by zero causes an error (Chapter 29) to be thrown for integer types, and results in infinity for floating point types.
NaN, PositiveInfinity, and NegativeInfinity are defined for the float and double types (float.NaN or double.PositiveInfinity, for example).
MaxValue and MinValue are defined for essentially all of the numeric types, which indicate the maximum and minimum values that a particular type can contain.
π and e are defined in the Math class, and can be accessed like this: float area = Math.PI * radius * radius;
Mathematical operations can result in numbers that go beyond the range of the type the value is being stored in. For integral types, this results in truncation (and wrapping around) while for floating point types, it results in PositiveInfinity or NegativeInfinity.
You can use the increment and decrement operators (++ and --) to add one or subtract one from a variable. For example, after the following, a will contain a value of 4: int a = 3;
a++;
54 Chapter 9 More Math
Here are the basic things we’re going to discuss in this chapter. We’ll start by talking about doing division with integers (“integer division”) and an interesting problem that comes up when we do that.
We’ll then talk about converting one type of data to another (called typecasting or simply casting).
Next we’ll talk about dividing by zero, and what happens in C# when you attempt this. We’ll then talk about a few cool special numbers in C#, like infinity, NaN, the number e, and π. Then we’ll take a look at overflow and underflow, and finish up with a cool and frequently used feature: incrementing and decrementing.
It’s probably not required to read all of this if you’re in a rush, but don’t skip the section on casting or the section on incrementing and decrementing. You’ll regret it if you do, I promise!
There’s a lot to learn here. Don’t worry if you don’t catch it all your first time. You can always come back later on, and in fact, having some experience behind you will probably make it make that much more sense, too.