beginner18 min

Variables, Types, and String Interpolation

Declaring variables with explicit types and `var`, C#'s basic types, and string interpolation.

What you'll learn

  • Declare variables with an explicit type and with `var` type inference
  • Identify C#'s basic types: int, double, string, bool, and const
  • Format interpolated string expressions, including numeric format specifiers like `:F1`

Explanation

C# is statically typed: every variable has a fixed type, checked at compile time. You can declare one with an explicit type (int age = 30;) or let the compiler infer the type from the value using var (var age = 30;) -- var is purely a compile-time convenience; the variable is still strongly typed as int, it's just that you didn't have to spell it out. var only works for local variables inside a method body, never for a class-level field.

C#'s common basic types include int (32-bit integer), double (64-bit floating point), string, bool, and decimal (a higher-precision type built for money and other values where floating-point rounding error is unacceptable). C# does not implicitly convert between most numeric types where precision could silently be lost -- assigning a double to an int variable requires an explicit cast ((int)someDouble), though C# does allow some implicit widening conversions (an int can be assigned directly to a double variable, since no information is lost).

A const is a compile-time constant, declared with const Type name = value; -- its value must be knowable at compile time and can never change.

String interpolation ($"...") embeds expressions directly inside a string literal using {expression}, optionally with a format specifier after a colon -- {heightCm:F1} formats a floating-point value with exactly one digit after the decimal point.

Guided lab

Predict: Variables, const, and formatted interpolation

C#Not executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints.

using System;

string name = "Ada";
int age = 30;
const double pi = 3.14159;

double height = 5.5;
double heightCm = height * 30.48;

Console.WriteLine($"{name} is {age} years old.");
Console.WriteLine($"Height in cm: {heightCm:F1}");
Console.WriteLine($"Pi is approximately {pi}");

Stuck? Get a hint.

Common mistakes

  • Assigning a `double` to an `int` variable without an explicit cast, expecting C# to truncate it automatically -- narrowing conversions require an explicit `(int)` cast.
  • Forgetting that `var` still produces a strongly-typed variable -- it's compile-time type inference, not a dynamic/untyped variable.
  • Omitting the format specifier (e.g. `:F1`) and being surprised interpolation prints a `double`'s full default precision instead of a rounded value.

Knowledge check

Knowledge check

1. What does `var age = 30;` produce in C#?
2. What must you do to assign a `double` value to an `int` variable in C#?
3. What does the format specifier in `$"{heightCm:F1}"` control?

Takeaway

Use `var` freely for local variables when the type is obvious from the value -- it's still fully statically typed -- and remember narrowing numeric conversions (like `double` to `int`) need an explicit cast.

Summary

C# variables are statically typed, declared with an explicit type or inferred with `var`; `const` fixes a compile-time value; string interpolation embeds expressions and optional format specifiers directly in a string literal.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.