Numeric types and literals
C# offers several numeric types because different values need different ranges and precision. Use an integral type for whole numbers, float or double for approximate binary floating-point measurements, and decimal when base-10 fractions are important. A literal is a value written directly in source, such as 42, 2.5, or 2.5m.
int boxes = 3;long totalBooks = 3_000_000_000L;byte labelsPerBox = 4;System.Console.WriteLine(boxes * labelsPerBox);System.Console.WriteLine(totalBooks);Small quantities are int or byte; the large count fits long. The byte is promoted to int in the multiplication.
Expected output (reasoned, not executed):
123000000000Whole-number types
The signed types sbyte, short, int and long use 8, 16, 32 and 64 bits. Their unsigned counterparts byte, ushort, uint and ulong represent nonnegative values. For example, byte ranges from 0 to 255 and int from -2,147,483,648 to 2,147,483,647. nint and nuint have native pointer-sized ranges, which depend on the target process. Choose int for ordinary small counts, long when a wider signed range is required. Larger types do not automatically prevent every overflow.
Numeric literal suffixes
An ordinary small whole-number literal such as 42 is int. 42L is long; 42U is uint; 42UL is ulong for values in the relevant ranges. Large unsuffixed integer literals may be inferred as a wider type. A real literal with a decimal point and no suffix is double by default. Write 1.5f for float or 1.5m for decimal. Underscores can make digits readable (1_000), and 0x and 0b introduce hexadecimal and binary integral literals.
Precision and a useful comparison
float and double are binary floating-point types with roughly 6–9 and 15–17 significant decimal digits of precision respectively; decimal offers roughly 28–29 significant digits and represents many base-10 fractions exactly. Neither unlimited precision nor exact representation of every fraction is guaranteed. Decimal has a much smaller exponent range than double. For monetary-style calculations, decimal is useful, but rounding rules still need to be specified. Use Math.Round later with an explicit policy when the domain requires rounding; the arithmetic examples here require no rounding.
Examples and walkthroughs
decimal unitPrice = 2.50m;int quantity = 4;decimal total = unitPrice * quantity;System.Console.WriteLine(total.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture));The m suffix chooses decimal. The last line fixes the decimal point and two displayed places independently of machine culture. The formatting API is a preview; it is taught later.
Expected output (reasoned, not executed):
10.00double approximate = 0.1 + 0.2;decimal exact = 0.1m + 0.2m;System.Console.WriteLine(approximate == 0.3);System.Console.WriteLine(exact == 0.3m);The == operator compares values (introduced in the next chapter). The usual .NET double computation rounds in binary, whereas these decimal fractions add exactly. This is a demonstration of these inputs, not a rule that all decimal operations are exact.
Expected output (reasoned, not executed):
FalseTrueCommon mistake and correction
decimal price = 2.50; is not a valid implicit assignment from a double literal. Use 2.50m. Do not use a narrow integer for unbounded totals. Be careful comparing computed floating-point values for exact equality; later lessons cover tolerance and checked arithmetic.
Try it yourself
Choose types for 18 items, a length of 1.25 meters, and a fictional unit price of 3.75. Display the exact total for 4 items with two decimal places. Explain why the literal suffix matters.
Next: Boolean, char, string and null. These examples are static study material; actual C# compilation and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Numeric types and literals — knowledge check
Apply the lesson concepts to the following scenarios.
Which type is a sensible default for a count of 18 books?
Which literal has type decimal?
Why does decimal often suit fictional price calculations?
What is the range of byte?
Which type is appropriate for the signed literal 3_000_000_000L?