HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C#VariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Dictionary
🔀 Condition & Loop
🧩 Function
C#›Variables

Variables

Variables with var and constants with const; specifying types and reassigning values.

Simple enough for a 5-year-old to understand

variable with var

If you use var, the type is inferred from the value. You can also write the type explicitly: int, double, string, bool.

Example code
1
using System;
2
var name = "Alex";
string variable.
Variablestring variable.
3
int age = 36;
Explicit type: int.
VariableExplicit type: int.
4
Console.WriteLine(name);
Alex
Output / InputAlex
5
Console.WriteLine(age);
36
Output / Input36
VariableOutput / Input
💡 Hint: var = a variable whose type is inferred from the value.

About this topic

C# is a strongly typed language: every variable has a type and that type does not change. However, you don't always have to write the type — with the var keyword, the compiler infers the type from the right side. This shortens the code without sacrificing type safety.

There are two categories in C#: value types and reference types. Value types — int, double, bool, struct — hold the value directly. Reference types — string, arrays, classes — point to an object. This distinction determines the copying behavior.

One of the most important innovations in Modern C# is nullability checking: you indicate that a reference can be null with a question mark, and the compiler warns you where you haven't checked for null. This moves the most common runtime error to compile-time.

What you will learn

  • Defining a variable by specifying the type and with var
  • The distinction between value type and reference type
  • The difference between const and readonly
  • Nullable types
  • The naming convention
  • The requirement of initial value assignment

When to use?

var avoids repetition when the type is obvious on the right side and is widely accepted in the .NET world. If the type improves readability, it is better to write it explicitly.

const is used for constants whose value is known at compile time, and readonly is used for constants to be assigned only in the constructor. The two serve different purposes and should not be confused.

Common mistakes

Wrong

int count;
Console.WriteLine(count);

Local variables must be initialized; the compiler gives "error CS0165: Use of unassigned local variable 'count'". This is the same rule as Java and does not apply to class fields.

Correct

int count = 0;
Console.WriteLine(count);

Wrong

var total = 10;
total = "on";

var performs type inference, it does NOT make the type flexible. total is now an int, and assigning text gives the "error CS0029: Cannot implicitly convert type 'string' to 'int'" error. var is a feature that has nothing to do with var in JavaScript.

Correct

var total = 10;
var label = "on";

Wrong

string name = null;
Console.WriteLine(name.Length);

Reading a property on null throws a "NullReferenceException". When nullability checking is enabled in Modern C#, the compiler warns about this beforehand; taking the warning into account prevents runtime crashes.

Correct

string? name = null;
Console.WriteLine(name?.Length ?? 0);

Frequently asked questions

Does using var break type safety?

No. var is just shorthand; the compiler determines the type from the right side and that type is fixed. It has nothing to do with JavaScript's var. If the type is obvious when looking at the right side, using var is a common and accepted choice.

What is the difference between const and readonly?

The const value MUST be known at COMPILE time and is embedded directly into the code. readonly, on the other hand, can be assigned in the constructor, meaning it can be a value calculated at runtime. Constants read from configuration are readonly.

What does the question mark mean?

It indicates that the type can be null. It adds the ability to hold null for value types; for reference types, it tells the compiler "this can be null, expect me to check it". Nullability checking is the way to catch NullReferenceException at compile-time.

Summary

Every variable in C# has a fixed type; var is only type inference, it does not bring flexibility. The distinction between value and reference types determines the copying behavior, and nullability checking brings NullReferenceException to compile-time.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code