Variables & types in C#
How C# declares things: inferred with var, spelled out, or locked with const.
// declarations: the compiler infers with var, or you spell the type
var greeting = "hello, C#";
int year = 2026;
double ratio = 21.0 / 34;
bool ready = year > 2000 && ratio < 1;
const string Version = "10.0";
Console.WriteLine($"{greeting} ({Version})");
Console.WriteLine($"ratio {ratio:F3}, ready: {ready}");
How it works
varasks the compiler to infer the type from the right-hand side.int,doubleandboolspell the type when inference would obscure it.constfixes a value at compile time; interpolation prints them all.
Keywords and builtins used here
boolconstdoubleintstringvar
The run, in numbers
- Lines
- 9
- Characters to type
- 312
- Tokens
- 49
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 31 seconds.
Step 1 of 3 in Variables & types, step 1 of 29 in Language basics.