C# Data Types
Understanding different types of data in C#
🎯 What are Data Types?
Data types specify the type of data a variable can hold in C#. They define the size and type of values, ensuring your program handles data correctly and efficiently.
// Different data types in C#
int age = 25; // Integer
double price = 19.99; // Decimal number
string name = "Alice"; // Text
bool isActive = true; // True/False
Console.WriteLine($"{name} is {age} years old");
Output:
Alice is 25 years old
Common Data Types
Integer Types
Whole numbers without decimals
int age = 30;
long population = 7800000000;
Floating-Point
Numbers with decimal points
double price = 19.99;
float temperature = 36.5f;
Text Types
Characters and strings
char grade = 'A';
string name = "John";
Boolean
True or false values
bool isActive = true;
bool hasAccess = false;
🔹 Integer Data Types
Integer types store whole numbers and vary by size, such as int, long, and short. Choose based on range requirements: int for general use, long for large values. For example, int age = 25; stores a typical age value efficiently within memory.
// Different integer types
int number = 100; // -2,147,483,648 to 2,147,483,647
long bigNumber = 9876543210; // Very large numbers
short smallNumber = 32000; // -32,768 to 32,767
byte age = 25; // 0 to 255
Console.WriteLine(number); // Output: 100
Console.WriteLine(bigNumber); // Output: 9876543210
Console.WriteLine(smallNumber); // Output: 32000
Console.WriteLine(age); // Output: 25
Output:
100
9876543210
32000
25
🔹 Floating-Point Data Types
Floating-point types like float, double, and decimal handle numbers with decimal points. Use double for general precision, decimal for financial accuracy. For example, double temperature = 36.5; represents a measured value with fractional precision.
// Floating-point types
float temperature = 36.5f; // 6-7 digits precision
double price = 19.99; // 15-16 digits precision
decimal money = 1234.56m; // 28-29 digits precision (for money)
Console.WriteLine(temperature); // Output: 36.5
Console.WriteLine(price); // Output: 19.99
Console.WriteLine(money); // Output: 1234.56
// Calculations
double result = 10.5 + 5.3;
Console.WriteLine(result); // Output: 15.8
Output:
36.5
19.99
1234.56
15.8
🔹 Character and String Types
The char type holds a single character, while string stores text sequences of any length. Use char for symbols like 'A' and string for names or messages, such as "Hello World". Strings are immutable and support various text operations.
// Character type (single character)
char grade = 'A';
char symbol = '$';
char letter = 'Z';
Console.WriteLine(grade); // Output: A
Console.WriteLine(symbol); // Output: $
// String type (text)
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName); // Output: John Doe
// String with special characters
string message = "Hello\nWorld"; // \n creates a new line
Console.WriteLine(message);
Output:
A
$
John Doe
Hello
World
🔹 Boolean Data Type
The bool type stores true or false values, driving conditional logic and decision-making in programs. It controls if statements, loops, and flags, like bool isLoggedIn = true;. Booleans are essential for representing binary states and enabling program flow.
// Boolean values
bool isActive = true;
bool hasPermission = false;
bool isAdult = true;
Console.WriteLine(isActive); // Output: True
Console.WriteLine(hasPermission); // Output: False
// Boolean expressions
int age = 20;
bool canVote = age >= 18;
Console.WriteLine(canVote); // Output: True
// Using booleans in conditions
if (isAdult)
{
Console.WriteLine("Access granted");
}
Output:
True
False
True
Access granted
🔹 Type Inference with var
Using var for local variables enables type inference, letting the compiler determine the data type automatically. For example, var number = 100; becomes int, var price = 19.99; becomes double, var name = "Alice"; is inferred as string, and var flag = true; becomes bool. This improves code readability and reduces verbosity, but can only be used when the variable is initialized at the declaration.
// Using var keyword (type inference)
var number = 100; // Inferred as int
var price = 19.99; // Inferred as double
var name = "Alice"; // Inferred as string
var isActive = true; // Inferred as bool
Console.WriteLine(number); // Output: 100
Console.WriteLine(price); // Output: 19.99
Console.WriteLine(name); // Output: Alice
Console.WriteLine(isActive); // Output: True
// var requires initialization
// var x; // Error: must initialize when using var
Output:
100
19.99
Alice
True
🔹 Default Values
In C#, uninitialized variables are assigned default values automatically. Numeric types like int and double default to 0. The bool type defaults to false, and reference types such as string default to null. This ensures variables have a predictable state before assignment, preventing undefined behavior in your programs.
// Default values for different types
int defaultInt = default; // 0
double defaultDouble = default; // 0.0
bool defaultBool = default; // False
string defaultString = default; // null
Console.WriteLine(defaultInt); // Output: 0
Console.WriteLine(defaultDouble); // Output: 0
Console.WriteLine(defaultBool); // Output: False
Console.WriteLine(defaultString == null); // Output: True
Output:
0
0
False
True