Skip to content

C# Built-in Types — int, double, bool, char, string, decimal, object, dynamic

DodaTech Updated 2026-06-28 8 min read

In this tutorial, you will learn about C# Built. We cover key concepts, practical examples, and best practices to help you master this topic.

C# provides a rich set of built-in types that map directly to .NET types, covering integers of various sizes, floating-point numbers for scientific computing, high-precision decimal for financial calculations, and special types for dynamic programming.

What You'll Learn

You will master every built-in type in C#: integral types (int, long, short, byte), floating-point types (float, double), decimal for financial precision, bool for boolean logic, char for characters, string for text, object for universal reference, and dynamic for runtime binding. You will understand ranges, precision limits, and appropriate use cases for each.

Why It Matters

Choosing the wrong type causes bugs: using float for currency leads to rounding errors, using int for database IDs that exceed 2 billion causes overflow, and using double for loop counters produces unexpected behavior. Mastering built-in types ensures correctness in your .NET applications and optimal memory usage.

Real-World Use

Financial applications use decimal to avoid rounding errors in Transaction processing. Game engines use float for 3D coordinates where precision beyond 7 digits is unnecessary. Scientific computing uses double for maximum floating-point precision. API responses often use object or dynamic for polymorphic Serialization.

Learning Path

graph LR
    A["04: Variables & Types"] --> B["05: Built-in Types"]
    B --> C["06: Operators"]
    C --> D["07: Control Flow"]
    D --> E["08: Loops"]
    style A fill:#4a90d9,stroke:#2c5f8a,color:#fff
    style B fill:#4a90d9,stroke:#2c5f8a,color:#fff
    style C fill:#4a90d9,stroke:#2c5f8a,color:#fff
    style D fill:#4a90d9,stroke:#2c5f8a,color:#fff
    style E fill:#4a90d9,stroke:#2c5f8a,color:#fff

Integral Types

Type Size Range Alias
byte 8 bits 0 to 255 System.Byte
sbyte 8 bits -128 to 127 System.SByte
short 16 bits -32,768 to 32,767 System.Int16
ushort 16 bits 0 to 65,535 System.UInt16
int 32 bits -2.1B to 2.1B System.Int32
uint 32 bits 0 to 4.3B System.UInt32
long 64 bits -9.2Q to 9.2Q System.Int64
ulong 64 bits 0 to 18.4Q System.UInt64
nint Platform Platform-dependant System.IntPtr
nuint Platform Platform-dependant System.UIntPtr
int population = 8_000_000_000;  // Digit separators
long universeAge = 13_800_000_000L;  // L suffix for long
byte small = 255;
short temperature = -10;
uint positiveOnly = 4_000_000_000U;  // U suffix for uint

// Check min/max values
Console.WriteLine($"int min: {int.MinValue}, max: {int.MaxValue}");
Console.WriteLine($"long min: {long.MinValue}, max: {long.MaxValue}");

Floating-Point Types

Type Size Precision Range
float 32 bits ~7 digits 1.5e-45 to 3.4e38
double 64 bits ~15 digits 5.0e-324 to 1.7e308
float gravity = 9.81f;          // f suffix for float
double pi = 3.14159265358979;  // Default for floating-point literals
double scientific = 1.23e-4;    // Scientific notation: 0.000123

// Precision demonstration
float f = 0.1f;
double d = 0.1;
Console.WriteLine($"float: {f:F20}");  // 0.10000000149011611938
Console.WriteLine($"double: {d:F20}"); // 0.10000000000000000555

Decimal Type

decimal is a 128-bit type with 28-29 significant digits, designed for financial calculations.

Type Size Precision Range
decimal 128 bits 28-29 digits 7.9e-28 to 7.9e28
decimal price = 19.99m;         // m suffix for decimal
decimal tax = 0.08m;
decimal total = price * (1 + tax);
Console.WriteLine($"Total: {total:C}");  // Currency format: $21.59

// Decimal avoids floating-point errors
decimal a = 0.1m;
decimal b = 0.2m;
Console.WriteLine(a + b);  // 0.3 (exact)
Console.WriteLine(0.1 + 0.2); // 0.30000000000000004 (double error)

Boolean Type

bool isComplete = true;
bool hasErrors = false;

// Boolean operations
bool result = isComplete && !hasErrors;  // true
bool orResult = isComplete || hasErrors; // true
bool xorResult = isComplete ^ hasErrors; // true (XOR)

// Boolean from comparison
bool isGreater = 10 > 5;  // true
bool isEqual = 3 == 3;    // true

Character Type

char represents a single Unicode character (16-bit UTF-16 code unit):

char letter = 'A';
char digit = '7';
char symbol = '$';
char unicode = '\u0041';  // 'A' via Unicode escape
char newline = '\n';      // Escape sequence
char tab = '\t';

Console.WriteLine($"Letter: {letter}, Code: {(int)letter}");
// Output: Letter: A, Code: 65

String Type

string is an immutable sequence of characters (reference type):

string name = "Alice";
string greeting = "Hello, " + name + "!";
string interpolated = $"Hello, {name}!";
string verbatim = @"C:\Users\Alice\Documents";
string multiline = """
    This is a raw string literal.
    It preserves indentation.
    """;

Console.WriteLine(greeting);       // Hello, Alice!
Console.WriteLine(interpolated);   // Hello, Alice!
Console.WriteLine(verbatim);       // C:\Users\Alice\Documents

Object Type

object is the universal base type for all types in C# (alias for System.Object):

object anything;
anything = 42;              // Boxing
anything = "Hello";
anything = 3.14;
anything = new List<int>();

// Type checking
if (anything is string str)
{
    Console.WriteLine($"It's a string: {str}");
}

object obj = 42;
int back = (int)obj;  // Unboxing

Dynamic Type

dynamic bypasses compile-time Type Checking, resolved at runtime:

dynamic value = 42;
Console.WriteLine(value + 10);   // 52

value = "Hello";
Console.WriteLine(value.Length); // 5

value = new { Name = "Test", Count = 3 };
Console.WriteLine(value.Name);    // Test (resolved at runtime)

Unlike object, dynamic operations are not resolved until runtime. This is useful for COM interop, dynamic languages, and scenarios where the type is unknown at compile time.

Type Literals and Suffixes

var a = 42;            // int
var b = 42L;           // long
var c = 42U;           // uint
var d = 42UL;          // ulong
var e = 42.0;          // double
var f = 42.0f;         // float
var g = 42.0m;         // decimal
var h = 42M;           // decimal (no decimal point)
var i = 0x2A;          // hexadecimal: 42
var j = 0b101010;      // binary: 42

Common Mistakes

Mistake 1: Using float or double for Currency

Floating-point types cannot represent decimal fractions exactly. Always use decimal for financial calculations to avoid rounding errors.

Mistake 2: Integer Division

int result = 5 / 2; gives 2, not 2.5. Use at least one double operand: 5 / 2.0 or cast: (double)5 / 2.

Mistake 3: Overflow Without Checking

int max = int.MaxValue; max++; silently wraps to int.MinValue. Use checked context or long for large values.

Mistake 4: Mixing Signed and Unsigned Types

Comparing an int with a uint requires implicit conversion. Both are 32-bit but have different ranges.

Mistake 5: Assuming object and dynamic Are the Same

object is statically typed; the compiler knows the type at compile time. dynamic defers all resolution to runtime. dynamic is slower but more flexible.

Mistake 6: Forgetting String Immutability

string s = "Hello"; s += " World"; creates a new string object. For many concatenations, use StringBuilder.

Practice Questions

  1. What is the difference between float, double, and decimal? When would you use each?
  2. Why does int result = 5 / 2 produce 2 instead of 2.5? How would you get the correct result?
  3. What is the range of an int type? What happens when you exceed it?
  4. Explain the difference between object and dynamic.
  5. What are digit separators in numeric literals? Give an example.

Challenge

Write a program that demonstrates the precision difference between float, double, and decimal by performing the same calculation (1 / 3) with each type and printing the results with 30 decimal places.

FAQ

What is the difference between `int` and `Int32`?

There is no difference. int is a C# alias for the System.Int32 type. Use int in code for readability. Both refer to the same 32-bit signed integer.

Why does `0.1 + 0.2` not equal `0.3` in floating-point?

Floating-point types use binary representation, and 0.1 cannot be represented exactly in binary. This is inherent to IEEE 754. Use decimal when exact decimal representation is required.

What is the maximum length of a string in C#?

Theoretical maximum is about 2 billion characters (based on Int32.MaxValue). Practical limits depend on available memory. Strings are reference types stored on the heap.

Can I use `var` with nullable types?

Yes. var x = (int?)42; infers int?. However, var x = null; does not compile because the compiler cannot infer the type from null alone.

What is the difference between `checked` and `unchecked` contexts?

In a checked context, arithmetic overflow throws an OverflowException. In the default unchecked context, overflow silently wraps. Use checked for critical calculations.

Mini Project

Create a type demonstration program:

Console.WriteLine("=== C# Type Explorer ===\n");

// Integer types
Console.WriteLine("--- Integers ---");
Console.WriteLine($"byte: {byte.MinValue} to {byte.MaxValue} ({sizeof(byte)} bytes)");
Console.WriteLine($"int: {int.MinValue} to {int.MaxValue} ({sizeof(int)} bytes)");
Console.WriteLine($"long: {long.MinValue} to {long.MaxValue} ({sizeof(long)} bytes)");

// Floating-point
Console.WriteLine("\n--- Floating-Point ---");
Console.WriteLine($"float: {float.MinValue} to {float.MaxValue} (precision: ~7 digits)");
Console.WriteLine($"double: {double.MinValue} to {double.MaxValue} (precision: ~15 digits)");

// Decimal
Console.WriteLine("\n--- Decimal ---");
Console.WriteLine($"decimal: {decimal.MinValue} to {decimal.MaxValue} (precision: 28 digits)");

// Division demonstration
Console.WriteLine("\n--- Division Precision ---");
Console.WriteLine($"int 5/2: {5 / 2}");
Console.WriteLine($"double 5/2: {5.0 / 2.0}");
Console.WriteLine($"decimal 5/2: {5.0m / 2.0m}");

// Character display
Console.WriteLine("\n--- Characters ---");
for (char c = 'A'; c <= 'Z'; c++)
{
    Console.Write(c);
}
Console.WriteLine();

Expected output:

=== C# Type Explorer ===

--- Integers ---
byte: 0 to 255 (1 bytes)
int: -2147483648 to 2147483647 (4 bytes)
long: -9223372036854775808 to 9223372036854775807 (8 bytes)

--- Floating-Point ---
float: -3.402823E+38 to 3.402823E+38 (precision: ~7 digits)
double: -1.7976931348623157E+308 to 1.7976931348623157E+308 (precision: ~15 digits)

--- Decimal ---
decimal: -79228162514264337593543950335 to 79228162514264337593543950335 (precision: 28 digits)

--- Division Precision ---
int 5/2: 2
double 5/2: 2.5
decimal 5/2: 2.5

--- Characters ---
ABCDEFGHIJKLMNOPQRSTUVWXYZ

What's Next

You have mastered all built-in types in C#. The next lesson covers operators: arithmetic, relational, logical, bitwise, and the null-conditional operator.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro