0Pricing
C# Academy · Lesson

Local Variable Type Inference with var

Use var where the type is obvious.

Local Variable Type Inference with var is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is var?

var lets the compiler infer a local variable type from the initializer. The variable is still strongly typed at compile time.

using System;

class Program {
    static void Main() {
        var count = 10;
        Console.WriteLine(count);
    }
}

var Is Not dynamic

var is resolved at compile time. var x = 10 is exactly an int and cannot later hold a string.

using System;

class Program {
    static void Main() {
        var name = "Ada";
        Console.WriteLine(name.GetType().Name);
    }
}

Inference from Literals

The compiler reads the literal to pick the type: 1 is int, 1.5 is double, "hi" is string.

using System;

class Program {
    static void Main() {
        var a = 1;
        var b = 1.5;
        var c = "hi";
        Console.WriteLine(a.GetType().Name + " " + b.GetType().Name + " " + c.GetType().Name);
    }
}

Initializer Is Required

Because the type comes from the initializer, var variables must be assigned when declared.

using System;

class Program {
    static void Main() {
        var total = 0;
        for (int i = 1; i <= 4; i++) total += i;
        Console.WriteLine(total);
    }
}

var with Obvious Types

var shines when the right side already states the type, like a constructor call. It avoids repetition.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var list = new List<int> { 1, 2, 3 };
        Console.WriteLine(list.Count);
    }
}

var with Method Results

You can use var for return values when the method name makes the type clear.

using System;

class Program {
    static int Square(int n) { return n * n; }
    static void Main() {
        var result = Square(6);
        Console.WriteLine(result);
    }
}

var in foreach

var is handy in foreach loops over collections of known element types.

using System;

class Program {
    static void Main() {
        var names = new[] { "Sam", "Lee" };
        foreach (var n in names)
            Console.WriteLine(n);
    }
}

Inferred Arrays

An implicitly typed array new[] { ... } infers its element type from the values.

using System;

class Program {
    static void Main() {
        var nums = new[] { 10, 20, 30 };
        Console.WriteLine(nums.Length);
    }
}

var Is Still Type-Safe

Because the type is fixed, assigning the wrong type later fails to compile, just like an explicit declaration.

using System;

class Program {
    static void Main() {
        var x = 5;
        x = 10;
        Console.WriteLine(x);
    }
}

var and Anonymous Types

var is required for anonymous types because they have no name you can write.

using System;

class Program {
    static void Main() {
        var point = new { X = 3, Y = 4 };
        Console.WriteLine(point.X + ", " + point.Y);
    }
}

Readability Benefit

Used well, var reduces noise and keeps code lined up, especially with long generic type names.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var scores = new Dictionary<string, int> { ["Ann"] = 90 };
        Console.WriteLine(scores["Ann"]);
    }
}

Quick Check

Test your understanding of var.

Recap

var tells the compiler to infer a local variable type from its initializer, so the variable stays strongly typed and fixed at compile time, unlike dynamic.

It requires an initializer, removes redundant type names, and is required for anonymous types. It improves readability when the type is already obvious.

Frequently asked questions

Is the “Local Variable Type Inference with var” lesson free?

Yes — the full text of “Local Variable Type Inference with var” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Local Variable Type Inference with var”?

Use var where the type is obvious. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Local Variable Type Inference with var” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Local Variable Type Inference with var
  2. When to Avoid var
  3. The dynamic Type
  4. Target-Typed new Expressions
← Back to C# Academy