English
Summary
Conclusions
Every value in C# has a type known at compile time. Value types (numbers, bool, char, structures) store values directly, while reference types (string, object, arrays, classes) store references to objects on the managed heap. The main integer type is int, or long for large values; use double for fractional calculations and decimal for money, as it avoids decimal fraction representation errors. Safe type conversions happen implicitly, while conversions that may lose data require an explicit cast or the Convert, Parse, or TryParse methods. When evaluating expressions, account for integer division, overflow, operator precedence, and the rounding mode; represent potentially absent values using types with ? and handle them with ?? and ??=.
Self-check questions
- How do value types differ from reference types? Give examples.
- What happens when assigning value-type and reference-type variables?
- What are boxing and unboxing? When does
InvalidCastExceptionoccur? - Which integer types does C# provide? How can you find a type's range?
- How do you write hexadecimal and binary literals? What are the
L,U,f, andmsuffixes for? - How do
float,double, anddecimaldiffer? Which type should you choose for money? - Why does
0.1 + 0.2 == 0.3produceFalse? How should you compare floating-point numbers? - What are
NaNand infinity? How can you check whether a value isNaN? - How does implicit typing with
varwork? When should you use it? - How does a
constconstant differ from a variable? - What do
int?andstring?mean? How do??and??=work? - Which type conversions happen implicitly? When is an explicit cast needed?
- How do
(int)3.5,Convert.ToInt32(3.5), andMath.Round(3.5)differ? - How do integer division and
%work? - What is the difference between prefix and postfix increment?
- What is overflow? How can you detect it with
checked? - What is short-circuit evaluation of
&&and||? - How do you test, set, and clear a bit using bitwise operators?
Useful links
- C# built-in types: https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/built-in-types
- Integer types: https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/integral-numeric-types
- Floating-point types: https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types
- Numeric conversions: https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/numeric-conversions
- Operators and expressions: https://learn.microsoft.com/dotnet/csharp/language-reference/operators/
checkedanduncheckedstatements: https://learn.microsoft.com/dotnet/csharp/language-reference/statements/checked-and-unchecked- Nullable types: https://learn.microsoft.com/dotnet/csharp/nullable-references
- Standard numeric formats: https://learn.microsoft.com/dotnet/standard/base-types/standard-numeric-format-strings