typestar

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

  1. var asks the compiler to infer the type from the right-hand side.
  2. int, double and bool spell the type when inference would obscure it.
  3. const fixes a value at compile time; interpolation prints them all.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Variables & types, step 1 of 29 in Language basics.

Next →

Variables & types in other languages