Skip to content
Learn Netverks

Lesson

Step 7/36 19% through track

variables-types-csharp

Variables and types

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
beginner

This lesson

This lesson teaches Variables and types: the syntax, patterns, and safety habits you need before advancing in C#.

Teams still ship Variables and types in C# codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Variables and types in contexts like: .NET services, Unity games, and Windows-centric tooling.

Write C# with Console.WriteLine (top-level or Program), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

C# variables have an explicit type: int count = 0;. Unlike JavaScript, you cannot silently change a variable from int to string without a compile error. var still infers a fixed type at compile time—like auto in C++.

Common scalar types

  • int, long — integers
  • double, float, decimal — floating point (decimal for money)
  • char — single UTF-16 code unit
  • booltrue or false
  • string — immutable text (reference type on the heap)

var and initialization

var count = 10;        // inferred as int
double ratio = 0.75;
bool ok = true;
string name = "Ada";

Value types live on the stack or inline in structs; reference types like string point to heap objects managed by the GC—contrast with manual memory in C++.

Important interview questions and answers

  1. Q: var vs dynamic?
    A: var is compile-time type inference; dynamic defers binding to runtime (rare in typical C#).
  2. Q: Value type vs reference type?
    A: Value types copy by value; reference types copy a reference—mutations through one reference affect the shared object.

Self-check

  1. What type does var infer from an integer literal?
  2. Is string a value type or reference type?

Pitfall: var still picks a fixed type at compile time—unlike dynamic typing in JavaScript.

Interview prep

What does var do?

Infers a fixed compile-time type from the initializer—still statically typed, not dynamic.

Value vs reference types?

Structs and primitives live on the stack or inline; classes are reference types allocated on the managed heap.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • decimal vs double?
  • var when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump