JS
C#

JavaScript to C#

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Async/Await4Array Methods → LINQ5Exception Handling6Collections7Generics8Delegates and Events9Records and Pattern Matching10File I/O
All Mirror Courses
JS
C#
Variables & Types
MirrorLesson 1 of 10
Lesson 1

Variables & Types

Static typing and type declarations

Introduction

In this lesson, you'll learn about variables & types in C#. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with static typing and type declarations.

C#
In C#:

C# has its own approach to static typing and type declarations, which we'll explore step by step.

The C# Way

Let's see how C# handles this concept. Here's a typical example:

C#
C# Example
string name = "Alice";
int age = 30;
double score = 9.5;
bool active = true;

const int Max = 100;

// var — type inference
var city = "Istanbul"; // inferred as string

// Types cannot change
int x = 42;
// x = "hello"; // compile error!

// Nullable types
int? maybeAge = null;

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
let name = "Alice";
let age = 30;
let score = 9.5;
let active = true;

const MAX = 100;

// Types can change
let x = 42;
x = "hello"; // valid in JS
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# is statically typed; variable types cannot change after declaration

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

JS let/const → C# type declarations or var (inferred)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# has bool (not JS's truthy/falsy); int, double, string are primitive types

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C#
In C#:

C# nullable types (int?) explicitly mark optional values

Step-by-Step Breakdown

1. Type Declarations

In C# every variable has a fixed type. The compiler verifies type correctness at compile time, catching errors before runtime.

JS
JavaScript
let age = 30;
C#
C#
int age = 30;

2. var Inference

C#'s var infers the type from the right side — like JS let, but the type is still fixed after declaration.

C#
C#
var name = "Alice";   // string, fixed
var count = 0;        // int, fixed

3. Nullable Types

C#'s ? suffix makes value types nullable, replacing JS's undefined/null pattern for optional values.

C#
C#
int? age = null;
if (age.HasValue) Console.WriteLine(age.Value);
int result = age ?? 0; // null-coalescing like JS ??

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • C# is statically typed; variable types cannot change after declaration
  • JS let/const → C# type declarations or var (inferred)
  • C# has bool (not JS's truthy/falsy); int, double, string are primitive types
Common Pitfall
Don't assume C# works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • C# types are fixed at declaration
  • var infers type but does not allow type changes
  • int? makes value types nullable
  • ?? null-coalescing works the same as JS
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in C# to practice these concepts.
OverviewNext