Variables
7 min · C#
Store values in named, typed boxes called variables.
Enemy 1 of 5
Typed variables
C# is a statically typed language: every variable's type is fixed at the moment you declare it, and the compiler checks that you never accidentally put the wrong kind of value into it. string holds text, int holds whole numbers — you write the type, then a name, then = and the value, ending with a semicolon.
This is a deliberate design choice that pays off as programs grow: because the compiler already knows every variable's type, it can catch a huge class of mistakes (like trying to add a number to a name by accident) before your program ever runs, rather than surprising you at runtime the way a dynamically typed language like Python or JavaScript might.
Once declared, a variable's name is used everywhere you'd otherwise write out its value, including inside Console.WriteLine(...). Naming things well here pays off — name is a much more readable stand-in for "Ada" once your programs get longer than a few lines.
Loading editor...