When to Use var vs Explicit Types in C#

var is a keyword for implicit typing where the compiler infers the type from the initialization expression. Explicit types declare the type directly in the code.

Use var when the type is obvious from the right side of the assignment (LINQ queries, new object creation with constructor). Use explicit types when the type isn't immediately clear, for better code readability.

Both compile to the same IL code - var is not dynamic typing. It's purely a compile-time feature that makes code more concise without sacrificing type safety.

C# Example Code
using System;
using System.Collections.Generic;
using System.Linq;

        // var - type is obvious from right side
        Console.WriteLine("=== Good use of var (obvious types) ===");
        var name = "Alice";  // Clearly a string
        var count = 42;  // Clearly an int
        var price = 19.99m;  // Clearly a decimal
        var isActive = true;  // Clearly a bool
        
        Console.WriteLine($"name is {name.GetType().Name}");
        Console.WriteLine($"count is {count.GetType().Name}");
        Console.WriteLine($"price is {price.GetType().Name}");

        // var with new keyword - type is obvious
        var person = new Person { Name = "Bob", Age = 30 };
        var numbers = new List<int> { 1, 2, 3, 4, 5 };
        var dictionary = new Dictionary<string, int>();
        
        Console.WriteLine($"\nperson is {person.GetType().Name}");

        // var with LINQ - reduces verbosity
        Console.WriteLine("\n=== var with LINQ ===");
        var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
        Console.WriteLine($"Even numbers: {string.Join(", ", evenNumbers)}");

        // var with anonymous types - required!
        var anonymous = new { FirstName = "Charlie", LastName = "Brown", Age = 25 };
        Console.WriteLine($"\nAnonymous type: {anonymous.FirstName} {anonymous.LastName}");
        // Can't use explicit type here - anonymous types have no name!

        // Explicit types - when type isn't obvious
        Console.WriteLine("\n=== Better with explicit types ===");
        int ParseNumber(string input) => int.Parse(input);
        double CalculateAverage(List<int> values) => values.Average();

        // Return type not obvious from method name
        int parsedNumber = ParseNumber("123");  // Better than var
        double average = CalculateAverage(numbers);  // Better than var
        
        Console.WriteLine($"Parsed: {parsedNumber}");
        Console.WriteLine($"Average: {average}");

        // Explicit types for clarity with interfaces
        IEnumerable<string> names = GetNames();  // Shows it's IEnumerable, not List
        List<string> namesList = GetNames().ToList();  // Shows we want a List
        
        Console.WriteLine($"\nnames is {names.GetType().Name}");
        Console.WriteLine($"namesList is {namesList.GetType().Name}");

        // var can hide important type information
        Console.WriteLine("\n=== When var can be confusing ===");
        var result = GetValue();  // What type is this? Not obvious!
        Console.WriteLine($"result is actually a {result.GetType().Name}");

        // Explicit is clearer here
        decimal explicitResult = GetValue();
        Console.WriteLine($"explicitResult is clearly a {explicitResult.GetType().Name}");

        // var with built-in type aliases
        Console.WriteLine("\n=== Mixing var with conversions ===");
        var text = "123";
        int number = int.Parse(text);  // Explicit shows conversion
        var convertedNumber = int.Parse(text);  // var hides that it's an int
        
        Console.WriteLine($"number: {number}");
        Console.WriteLine($"convertedNumber: {convertedNumber}");

        // Complex types benefit from var
        Console.WriteLine("\n=== var reduces repetition ===");
        
        // Verbose
        Dictionary<string, List<Person>> explicitDict = new Dictionary<string, List<Person>>();
        
        // Cleaner
        var cleanDict = new Dictionary<string, List<Person>>();
        
        Console.WriteLine("Both are Dictionary<string, List<Person>>");

static IEnumerable<string> GetNames()
{
    return new List<string> { "Alice", "Bob", "Charlie" };
}

static decimal GetValue()
{
    return 123.45m;
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Guidelines

Use var when:

  • Type is obvious from right side
  • Using 'new' keyword
  • Working with anonymous types
  • Type name is very long and repetitive

Use explicit types when:

  • Type isn't obvious from right side
  • Calling methods without 'new'
  • Type information aids readability
  • Working with numeric literals that need precision