This chapter is from the book
Workshop
Quiz
- What are the three primary groups C# types are divided into?
- Which predefined type is useful for financial calculations and why?
- What is a base type for all the predefined types?
- Why is the inclusion of a distinct bool type important?
- Is all string and character data stored as Unicode?
- What are the implications of strings being immutable?
- What is the difference between a prefix increment and a postfix increment operation?
- Can the null-coalescing operator (??) be used with reference types and nullable value types?
- Explain what happens during a boxing operation.
- Can a long be implicitly converted to an int?
Answers
- Types in C# are divided into reference types, value types, and type parameter types.
- The decimal type is useful for financial calculations because it eliminates many representation errors commonly found with other floating-point types.
- All the predefined types and everything in C# ultimately derive from the object type.
- By including a distinct bool type, C# helps eliminate several common programming errors by eliminating the ambiguity that can arise when using an integer 0 or 1 value.
- Yes, all strings and characters in C# are stored as Unicode code units, allowing them to be localized.
- Because strings are immutable, they cannot be changed after given a value. This means that any string concatenation operations result in creating an entirely new string object to hold the new value. Performing a large number of these operations in a repetitive fashion over a short period of time can lead to significantly increased memory usage and should be done using a StringBuilder instead.
- In a prefix increment operation, the result is the value of the variable before the increment; in a postfix increment operation, the result is the incremented value assigned back to the variable.
- Yes, the null-coalescing operator can be used with any type that can contain a null, including objects.
- A boxing operation occurs when a value type is used as a reference type and involves creating a new instance to hold the boxed value. Operations on a boxed object do not affect the original value.
- No, a long cannot be implicitly converted to an int because it would lose precision; it can, however, be explicitly converted.