Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# var Keyword


Share

The var Keyword

So far, you have created variables by writing the type first, like int or string.

C# can also find the type by itself. When you use the var keyword, the compiler (the tool that turns your code into a program) looks at the value, and picks the type for you.

For example, instead of writing int myNum = 5;, you can write:

Example

var myNum = 5;
Console.WriteLine(myNum);

Try it Yourself »

C# sees that 5 is a whole number, so myNum becomes an int. It works exactly like int myNum = 5;.


Different Types

The type depends on the value you give the variable:

Example

var myNum = 5;           // int
var myDoubleNum = 5.99;  // double
var myLetter = 'D';      // char
var myBool = true;       // bool
var myText = "Hello";    // string

Console.WriteLine(myNum);
Console.WriteLine(myDoubleNum);
Console.WriteLine(myLetter);
Console.WriteLine(myBool);
Console.WriteLine(myText);

Try it Yourself »



var Needs a Value

When you use var, you must give the variable a value right away. Without a value, C# cannot know which type to pick:

Example

var score;

The compiler shows an error like this:

prog.cs(9,7): error CS0818: An implicitly typed local variable declarator must include an initializer

Try it Yourself »

Implicitly typed means that C# picks the type, and an initializer is the value you give a variable when you create it.

To fix the error, give score a value: var score = 10;


The Type Stays the Same

Once C# has picked the type, it does not change. You can give the variable a new value, but only a value of the same type:

Example

var score = 10;  // score is an int
score = 20;      // OK, 20 is also an int
score = 9.5;     // Error, an int cannot store decimals

When to Use var

In this tutorial, we usually write the type, like int or string. It shows the reader right away what kind of value the variable stores.

The var keyword is most useful when the type has a long name. Later, you will learn about lists, which have long type names. Compare these two lines:

Example

// Without var
List<string> cars = new List<string>();

// With var
var cars = new List<string>();

Both lines create the same list. With var, you don't have to write List<string> twice.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.