Chapter 2 – Speaking C# Flashcards

1
Q

What statement can you type in a C# file to discover the compiler and language version?

A

error version

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two types of comments in C#?

A

The two types of comments in C# are a single-line comment prefixed with // and a
multi-line comment starting with /* and ending with */. There are also XML comments, which
are introduced in Chapter 4, Writing, Debugging, and Testing Functions, so I would not expect
you to know about them yet.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between a verbatim string and an interpolated string?

A

A verbatim string is prefixed with the @ symbol and each character (except “) is interpreted as itself; for example, a backslash \ is a backslash . An interpolated string is prefixed
with the $ symbol and can include expressions surrounded with braces like this: {expression}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why should you be careful when using float and double values?

A

You should be careful when using float and double values because they are not
guaranteed to be accurate, especially when performing equality comparisons.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you determine how many bytes a type like double uses in memory?

A

You can determine how many bytes a type like double uses in memory by using the
sizeof() operator, for example, sizeof(double).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When should you use the var keyword?

A

You should only use the var keyword to declare local variables when you cannot
specify a known type. It is easy to overuse var due to its convenience when initially writing
code, but its use can make it harder to maintain code later.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the newest syntax to create an instance of a class like XmlDocument?

A

The newest way to create an instance of a class like XmlDocument is to use a target-typed
new expression, as shown in the following code:

XmlDocument doc = new();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why should you be careful when using the dynamic type?

A

You should be careful when using the dynamic type because the type of object stored
in it is not checked until runtime, which can mean runtime exceptions being thrown if you
attempt to use a member that does not exist on the type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you right-align a format string?

A

To right-align a format string, after the index or expression, add a comma and an
integer value to specify a column width within which to align the value. Positive integers mean
right-aligned and negative integers mean left-aligned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What character separates arguments for a console app?

A

The space character separates arguments for a console app.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What type would you choose for the following “numbers”?
1. A person’s telephone number.
2. A person’s height.
3. A person’s age.
4. A person’s salary.
5. A book’s ISBN.
6. A book’s price.
7. A book’s shipping weight.
8. A country’s population.
9. The number of stars in the universe.
10. The number of employees in each of the small or medium businesses in the United Kingdom
(up to about 50,000 employees per business).

A
  1. Answer: string.
  2. Answer: float or double.
  3. Answer: int for best performance on most CPUs or byte (0 to 255) for the smallest size.
  4. Answer: decimal.
  5. Answer: string.
  6. Answer: decimal.
  7. Answer: float or double.
  8. Answer: uint (0 to about 4 billion).
  9. Answer: ulong (0 to about 18 quadrillion) or System.Numerics.BigInteger (allows an arbitrarily large integer).
  10. Answer: Since there are hundreds of thousands of small or medium businesses, we need to
    take memory size as the determining factor, so choose ushort, because it only takes 2 bytes
    compared to an int, which takes 4 bytes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly