Literals in C# are fixed values that appear directly in source code. Unlike variables, they lack identifiers and represent data directly within expressions and assignments.
Types of Literals
Integer Literals
Integer literals support three notation formats: decimal, octal, and hexadecimal. The prefix 0x indicates hexadecimal notation, while plain digits represent decimal values.
int decValue = 42;
int octValue = 052;
int hexValue = 0x2A;
Floating-Point Literals
Floating-point literals contain a decimal point or scientific notation. The f or F suffix designates a single-precision float, while d or D marks double precision.
float temperature = 98.6f;
double scientific = 6.02E23;
double percentage = 0.85d;
Boolean Literals
Boolean literals accept only two values: true and false. These primitive values control conditional logic throughout programs.
bool hasData = true;
bool isEmpty = false;
Character Literals
Character literals enclose a single character within single quotes. Escape sequences like \n for newline and \t for tab are also valid.
char grade = 'B';
char newline = '\n';
char unicodeChar = '\u0041';
String Literals
String literals wrap character sequences in double quotes. Verbatim strings prefixed with @ ignore escape sequences and allow multi-line content.
string greeting = "Welcome";
string path = @"C:\Users\Documents";
string multiline = @"Line one
Line two";
Literals vs Constants
Both literals and constants represent fixed values, but they differ in key aspects.
Constants receive symbolic names through the const keyword. The compiler substitutes their values during compilation, storing them in assembly metadata rather then runtime memory. Constants are ideal for values that remain unchanged across the codebase.
Literals exist directly in code with out names. They occupy memory when expressions execute at runtime. Literasl suit values used infrequently or in single locations.
const int MaxRetries = 5;
const string ApplicationName = "DataProcessor";
int currentCount = 1; // literal used once
int maxAllowed = MaxRetries; // constant reference
Memory Behavior
The compiler embeds constant values into metadata at compile time. This means constants incur no per-execution memory allocation—they live in the compiled assembly itself.
Literal values allocate memory when their containing expressions run. For instance, when assigning "Hello" to a variable, the string memory is allocated from the heap at runtime. Primitive numeric literals may optimize to stack allocations or immediate values in CPU instructions depending on context.
When to Use Each Approach
Apply literals directly when values appear in isolated expressions and rarely change:
if (count > 100) { ... }
result = price * 1.08;
Define constants when the same value appears in multiple locations or requires meaningful naming:
const double TaxRate = 0.08;
const int BufferSize = 256;
const string DefaultEncoding = "UTF-8";
Constants provide compile-time type checking and prevent accidental modification. Literals offer brevity for one-off values that need no documentation through naming.