Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 3 / 68

Working with Variables

In this lesson you will learn the basic way to use variables in C#, and how string interpolation keeps your code readable. This is for anyone searching for "C# variables string interpolation".

A variable is like a labelled box that stores a value under a name. Writing $"text {variableName}" (string interpolation) lets you build a string with values embedded directly in it.

The sample code declares two variables, string name = "Alice" and int age = 14, then assembles a message with the interpolated string $"Hello, {name}! You are {age} years old". Notice how the types string and int are written explicitly.

A common stumbling block for beginners is deciding when to use the var keyword instead. Declaring the type explicitly gives your IDE strong support for completion and error detection, but var has its place too, and it can be hard to know which one fits.

In real-world development, naming variables so that the purpose of the value is obvious makes your code far easier to understand when you come back to it later.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)