C#•Variables
C# Strongly Typed Implicit Var Assignments and Data Types
C# is a strictly typed language. When using the var keyword, the Roslyn compiler deduces the exact, immutable static type at compile time from the right-hand initialization expression. Unlike dynamic or JavaScript var, C# var provides 100% type safety and performance with zero runtime overhead.
Syntax
var clusterId = 404;
var nodeLatency = 14.85;
var isOperational = true;
decimal transactionBalance = 9450.75m;
Parameters & Values
Key components and acceptable values for this structure:
| Parameter | Type | Description |
|---|---|---|
| var | Keyword | Instructs compiler to infer the static type from initialization expression. |
| decimal (m) | Type | 128-bit high-precision floating point type for currency and exact financial math. |
| {val:Fx} | Formatting | String interpolation specifier formatting numbers to x fixed decimal places. |
Ready-to-Run Code Example
Executable in CompilraRun this complete, working example directly without installing C# locally:
using System;
public class Program {
public static void Main() {
// 1. Strongly-typed implicit var declarations
var clusterName = "Compilra-Production-Node";
var activeWorkers = 32;
var throughputRatio = 98.75;
var isOnline = true;
// 2. High-precision monetary decimal type
decimal financialReserve = 1254300.50m;
// 3. Inspecting resolved compile-time types via reflection
Console.WriteLine("=== C# Static Type Inference System ===");
Console.WriteLine($"Cluster: {clusterName} [Type: {clusterName.GetType().Name}]");
Console.WriteLine($"Workers: {activeWorkers} [Type: {activeWorkers.GetType().Name}]");
Console.WriteLine($"Throughput: {throughputRatio:F2}% [Type: {throughputRatio.GetType().Name}]");
Console.WriteLine($"Online: {isOnline} [Type: {isOnline.GetType().Name}]");
Console.WriteLine($"Reserves: ${financialReserve:N2} [Type: {financialReserve.GetType().Name}]");
}
}Expected Output:
=== C# Static Type Inference System === Cluster: Compilra-Production-Node [Type: String] Workers: 32 [Type: Int32] Throughput: 98.75% [Type: Double] Online: True [Type: Boolean] Reserves: $1,254,300.50 [Type: Decimal]
How It Works: Step-by-Step
- Lines 6-9: Uses var to infer string, int, double, and bool types at compile time.
- Line 12: Initializes 128-bit decimal with mandatory 'm' suffix for precise financial calculations.
- Lines 16-20: Uses string interpolation ($"") and GetType().Name to verify static compile-time types.
Key Concepts
- In C#, var is purely syntactic sugar: the compiler resolves the concrete type at build time, resulting in identical IL bytecode to explicit declarations.
- Once inferred, a var variable cannot be reassigned to a different type (e.g. assigning a string to an int var causes a compilation error).
- The decimal type provides 28-29 significant digits of precision, avoiding binary floating point approximation bugs inherent in float and double.
- String interpolation formatting like {val:N2} adds thousands separators and formats to 2 decimal places.
Common Mistake: Declaring var without an initialization expression
Because the compiler must deduce the type from the right-hand value, declaring an uninitialized var is a compile-time error.
❌ Incorrect
var totalJobs; // Compile Error: Implicitly-typed variables must be initialized
✅ Correct
var totalJobs = 0; // Correct: compiler infers int
Related C# Tutorials & Compiler
C# Operators: Null-Conditional (?.) and Null-Coalescing (??)
Master C# operators, arithmetic, comparison, null-conditional (?.), and null-coalescing (??).
C# Console Input and Output: Console.WriteLine and ReadLine
Print formatted output and read user inputs from standard console streams.
C# Type Casting: int.TryParse, as, and is Pattern Matching
Safely parse strings and cast objects using int.TryParse, as operator, and is pattern matching.
C# Online Compiler
Open in Compilra IDE
Run and test C# on Compilra
Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.