C#:C# Strongly Typed Implicit Var Assignments and Data Types
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:

ParameterTypeDescription
varKeywordInstructs compiler to infer the static type from initialization expression.
decimal (m)Type128-bit high-precision floating point type for currency and exact financial math.
{val:Fx}FormattingString interpolation specifier formatting numbers to x fixed decimal places.

Ready-to-Run Code Example

Executable in Compilra

Run 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

Run and test C# on Compilra

Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.

C# Online Compiler