When to Avoid var
Keep code readable by typing explicitly when needed.
When to Avoid var is a free C# Academy lesson on CoddyKit — lesson 2 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.
var Can Hide the Type
var is convenient, but it can obscure what a variable actually is, hurting readability for the next reader.
using System;
class Program {
static void Main() {
var x = Compute();
Console.WriteLine(x);
}
static int Compute() { return 42; }
}Unclear Method Returns
When a method name does not reveal the return type, prefer an explicit type so readers know what they have.
using System;
class Program {
static double Process() { return 3.14; }
static void Main() {
double value = Process();
Console.WriteLine(value);
}
}Number Type Surprises
var sum = 0 is an int. If you expect a double, that can cause integer division bugs.
using System;
class Program {
static void Main() {
double sum = 0;
sum += 7 / 2.0;
Console.WriteLine(sum);
}
}Be Explicit for Interfaces
If you want to program against an interface, declare it explicitly; var would pick the concrete type instead.
using System;
using System.Collections.Generic;
class Program {
static void Main() {
IList<int> items = new List<int> { 1, 2 };
Console.WriteLine(items.Count);
}
}Distant Initializers
When the initializer is a long or nested expression, an explicit type documents intent better than var.
using System;
class Program {
static void Main() {
int finalScore = (10 + 5) * 2 - 3;
Console.WriteLine(finalScore);
}
}Avoid for Primitive Clarity
Many teams write the explicit type for simple primitives so the intended kind of number is unmistakable.
using System;
class Program {
static void Main() {
long bigCount = 5000000000;
Console.WriteLine(bigCount);
}
}var Cannot Be a Field
var is only for local variables. Fields, parameters, and return types must always be explicit.
using System;
class Program {
static int field = 100;
static void Main() {
Console.WriteLine(field);
}
}Mixed Numeric Inference
Be careful: var avg = total / count with two ints stays an int. Make the intent explicit when you need decimals.
using System;
class Program {
static void Main() {
int total = 7, count = 2;
double avg = (double)total / count;
Console.WriteLine(avg);
}
}When Style Guides Differ
Some guides say "use var only when the type is obvious on the right side". Following a consistent rule helps a whole team.
using System;
class Program {
static void Main() {
var customer = new string("Ann"[0], 3);
Console.WriteLine(customer);
}
}Balance Convenience and Clarity
The goal is readable code. Use var where it helps and explicit types where they clarify.
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var lookup = new Dictionary<int, string>();
int key = 1;
lookup[key] = "one";
Console.WriteLine(lookup[1]);
}
}A Practical Rule of Thumb
If you cannot tell the type by reading the line alone, write it out. Otherwise var is fine.
using System;
class Program {
static void Main() {
var greeting = "Hello";
decimal price = 19.99m;
Console.WriteLine(greeting + " " + price);
}
}Quick Check
Test your judgment on when to avoid var.
Recap
Avoid var when it hides an important type, when the numeric kind matters (int vs double), when you want to target an interface, or when a method name does not reveal its return type.
var only works for local variables. A good rule: if the line alone does not reveal the type, write it explicitly.
Frequently asked questions
Is the “When to Avoid var” lesson free?
Yes — the full text of “When to Avoid 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 “When to Avoid var”?
Keep code readable by typing explicitly when needed. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “When to Avoid 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.