<aside> Understanding data types is fundamental to C# programming. This guide covers all essential data types and their practical applications.
</aside>
When using decimal numbers in C#, always use dots (.) rather than commas (,) as decimal separators.
float
(32-bit, 4 bytes)
• 7 decimal digits precision
• For basic calculations
• Suffix 'f' required (e.g., 3.14f)double
(64-bit, 8 bytes)
• 15-16 decimal digits precision
• Default for decimal numbers
• No suffix needed (e.g., 3.14)decimal
(128-bit, 16 bytes)
• 28-29 decimal digits precision
• For financial calculations
• Suffix 'm' required (e.g., 3.14m)Type | Size | Range | Usage |
---|---|---|---|
sbyte | 8-bit | -128 to 127 | Very small numbers |
short | 16-bit | -32,768 to 32,767 | Small numbers |
int | 32-bit | -2^31 to 2^31-1 | Most common choice |
long | 64-bit | -2^63 to 2^63-1 | Very large numbers |
char letter = 'A'; // Single character
char symbol = '$'; // Special character
char digit = '7'; // Numeric character
string name = "John"; // Basic string
string path = @"C:\\Program Files"; // Verbatim string
string interpolated = $"Hello {name}"; // String interpolation
string text = "Hello World";
int length = text.Length; // Get length
string upper = text.ToUpper(); // Convert to uppercase
string lower = text.ToLower(); // Convert to lowercase
string sub = text.Substring(0, 5); // Get "Hello"
bool contains = text.Contains("World"); // Check content
string[] words = text.Split(' '); // Split into array
Represents true/false values, essential for control flow and decision-making.
bool isValid = true;
bool hasError = false;
// Common boolean operations
bool andResult = isValid && hasError; // AND
bool orResult = isValid || hasError; // OR
bool notResult = !isValid; // NOT