C-SHARP - 2.3 Types and constant

C# offers different categories of data types. The primitive types are built into the language: integers, real numbers, characters and booleans. The most common ones are byte (0 to 255), short (-32 768 to 32 767), int (around ±2 billion), long (much larger), float, double, decimal, char and bool. Each maps internally to a .NET type — an int is an Int32 — and occupies a fixed number of bytes in memory. The full list is longer, but these are the ones you will use 95% of the time.

One peculiarity matters for real numbers. The default real type is double. If you want a float, append F after the literal (for example 3.14F) — otherwise the compiler treats it as a double and refuses the implicit narrowing. Same idea for decimal, where the suffix is M. Beyond primitives, C# also has non-primitive types: string, arrays, classes, structs — covered later.

Constants — variables that cannot change

  • Declared with const before the type: const int VieMin = 0;, const int VieMax = 100;.
  • The value is fixed at compile time and any attempt to reassign it is a compiler error.
  • Use them for values that never change during execution: max HP in a game, VAT rate (20% in France), mathematical constants.

Concrete demo: in a combat game the player has HP between 0 and 100. VieMin and VieMax are constants because they never change. vie, the current HP, is a regular variable because it shrinks when the player takes damage: int vie = 50; vie -= 10; Console.WriteLine(vie); prints 40. Trying VieMax = VieMax - 15; instead would fail compilation: a const cannot be reassigned. That is exactly the protection constants exist to provide — they freeze the values that must remain stable.

Summary

This lesson covers the fundamental data types available in C# and the concept of constants. You'll learn about primitive types (integers, floating-point numbers, characters, and booleans) with their storage sizes and valid ranges, as well as how to properly declare floats and decimals with the correct suffixes. The lesson also explains constants—immutable values that cannot be changed after initialization—and demonstrates their practical use with real-world examples like max/min health points in a game.

Key points

  • C# primitive types include byte, short, int, long (integers), float, double, decimal (real numbers), char (characters), and bool (true/false)
  • Storage size matters: byte uses 1 byte, short uses 2 bytes, int uses 4 bytes, long uses 8 bytes
  • Float literals must include the 'f' suffix (e.g., 3.14f) to avoid compiler errors; decimal requires 'm' suffix
  • Non-primitive types in C# include strings, arrays, classes, structs, enums, and interfaces
  • Constants are immutable values that cannot be modified after declaration, unlike variables
  • Constants are ideal for fixed values like maximum/minimum bounds in games or unchanging program parameters

FAQ

Why do I need to add 'f' suffix when declaring float values?

By default, the C# compiler treats decimal literals as double. Adding 'f' tells the compiler to treat the value as a float instead, preventing type mismatch errors.

What's the difference between a constant and a variable?

Variables can be modified throughout the program, while constants have a fixed value that cannot be changed after initialization, making them useful for values that should never be altered.

Which data type should I use for most integer values?

For most integer values, use int, which takes 4 bytes and can store values from about -2 billion to 2 billion. Use byte for very small ranges (0–255), short for -30,000 to 32,000, and long for very large numbers.