← C# from Scratch
Lesson 4 of 39 · 10 XP
Variables
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
A variable is a named memory cell of a particular type:
int lives = 3; // a whole number (32 bits)
double speed = 2.5; // a floating-point number
char grade = 'A'; // one character — single quotes
string name = "Kai"; // a string — double quotes
bool alive = true; // true / false
const int MaxLives = 5; // a constant: it can't be changed
var score = 100; // the compiler infers the type (int here) — but it's still strict
- C# is a strongly, statically typed language: you can't put a string into an
int; you get an error at compile time; - a name starts with a letter or
_, and local variables are customarily written incamelCase; - you must assign a value to a variable before using it — otherwise you get error CS0165.
String interpolation — $ before the string and expressions in { }:
Console.WriteLine($"{name}: {lives} lives, speed {speed}");
Task. Create the variables name = "Alex", level = 7, hp = 92.5 and print Alex — level 7, HP 92.5.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.