C# in A Nutshell - Code Listings - 4
C# in A Nutshell - Code Listings - 4
Syntax Basics
Type Basics
Numeric Types
Numeric Types
// The unsigned integral types are byte, ushort, uint and ulong:
byte b = 255;
b.Dump();
Numeric Literals
// Integral literals can use decimal or hexadecimal notation; hexadecimal is denoted with the 0x prefix:
int x = 127;
long y = 0x7F;
//From C# 7, you can insert an underscore anywhere inside a numeric literal to make it more readable:
int million = 1_000_000;
//C# 7 also lets you specify numbers in binary with the 0b prefix:
var b = 0b1010_1011_1100_1101_1110_1111;
//Real literals can use decimal and/or exponential notation. For example:
double d = 1.5;
double doubleMillion = 1E06;
Numeric Suffixes
long i = 5; // No suffix needed: Implicit lossless conversion from int literal to long
// The D suffix is redundant in that all literals with a decimal point are inferred to be double:
double x = 4.0;
Numeric Conversions
// Integral conversions are implicit when the destination type can represent every possible value
// of the source type. Otherwise, an explicit conversion is required:
https://www.albahari.com/nutshell/E12-CH02.aspx 1/4
4/18/24, 1:15 AM C# in a Nutshell - Code Listings
int x = 12345; // int is a 32-bit integral
long y = x; // Implicit conversion to 64-bit integral
short z = (short)x; // Explicit conversion to 16-bit integral
// Implicitly converting a large integral type to a floating-point type preserves magnitude but may
// occasionally lose precision:
int i1 = 100000001;
float f1 = i1; // Magnitude preserved, precision lost
int i2 = (int)f1; // 100000000
// The increment and decrement operators (++, --) increment and decrement numeric types by 1.
// The operator can either precede or follow the variable, depending on whether you want the
// value before or after the increment/decrement:
int x = 0, y = 0;
Console.WriteLine (x++); // Outputs 0; x is now 1
Console.WriteLine (++y); // Outputs 1; x is now 1
Integral Division
int a = 2 / 3; // 0
int b = 0;
int c = 5 / b; // throws DivisionByZeroException
Integral Overflow
int a = int.MinValue;
a--;
Console.WriteLine (a == int.MaxValue); // True
Overflow Checking
int a = 1000000;
int b = 1000000;
https://www.albahari.com/nutshell/E12-CH02.aspx 2/4
4/18/24, 1:15 AM C# in a Nutshell - Code Listings
// The 8- and 16-bit integral types are byte, sbyte, short, and ushort. These types lack their
// own arithmetic operators, so C# implicitly converts them to larger types as required.
// This can cause a compile-time error when trying to assign the result back to a small integral type:
short x = 1, y = 1;
short z = x + y; // Compile-time error
// In this case, x and y are implicitly converted to int so that the addition can be performed.
// To make this compile, we must add an explicit cast:
// Reminder when using LINQPad: You can highlight any section of code and
// hit F5 to execute just that selection!
// Unlike integral types, floating-point types have values that certain operations treat specially,
// namely NaN (Not a Number), +∞, −∞, and −0:
Console.WriteLine (double.NegativeInfinity); // -Infinity
// When using ==, a NaN value is never equal to another value, even another NaN value:
Console.WriteLine (0.0 / 0.0 == double.NaN); // False
// To test whether a value is NaN, you must use the float.IsNaN or double.IsNaN method:
Console.WriteLine (double.IsNaN (0.0 / 0.0)); // True
// Unlike decimal, float and double can cannot precisely represent numbers with a base-10
// fractional component:
{
float x = 0.1f; // Not quite 0.1
Console.WriteLine (x + x + x + x + x + x + x + x + x + x); // 1.0000001
}
{
decimal y = 0.1m; // Exactly 0.1
Console.WriteLine (y + y + y + y + y + y + y + y + y + y); // 1.0
}
// Neither double nor decimal can precisely represent a fractional number whose base 10
// representation is recurring:
Arrays
https://www.albahari.com/nutshell/E12-CH02.aspx 3/4
4/18/24, 1:15 AM C# in a Nutshell - Code Listings
Expressions and Operators
Null Operators
Statements
Namespaces
C# 12
in a Nutshell
About the Book
Code Listings
C# 12 in a Nutshell
C# 10 in a Nutshell
C# 9.0 in a Nutshell
C# 8.0 in a Nutshell
C# 7.0 in a Nutshell
Extras
Contact
https://www.albahari.com/nutshell/E12-CH02.aspx 4/4