Skip to content

Variables

In TypeScript and JavaScript, variables are dynamically typed and can hold values of any type unless explicitly constrained. You declare them using var, let, or const, with let and const being block-scoped and preferred for modern development. TypeScript introduces static typing through type annotations (e.g., let x: number = 10;), but at runtime, JavaScript remains dynamically typed, allowing variables to change types freely.

C#, on the other hand, is a statically typed language where variable types are enforced at compile time and runtime. Variables are typically declared with explicit types (int x = 10;), ensuring type safety. However, C# also supports type inference using var (var x = 10;), where the compiler infers the type but does not allow reassignment to a different type. Unlike JavaScript, variables in C# are strongly typed, meaning once a variable is declared with a type, it cannot hold a value of another type without explicit conversion.

Note on this guide

In this guide, I'll primarily use let so that the semantics match .NET var to make it clear how the code works. In practice, there are many cases where I would normally use const in JS/TS.

Inferred Types

ts
var x = 1;  // Hoisted
let x = 1;  // Block scope
const x = 1;  // Block scope; immutable
csharp
var x = 1;  // Block scope
const x = 1;  // Compiler "inlined"; NOT the same as JS const

Use C# record classes for immutability

C#'s const keyword does not mean the same thing as in JS. See the docs to understand the const designator in C#.

To achieve immutability, use C# record class classes with positional properties (which we'll visit later in Classes and Types).

Explicit Types

ts
// Primitives
let x:number = 1;
let y:string = "";

// Reference types
let map = new Map();
csharp
// Primitives
int x = 1;
string y = "";

// Reference types
var map = new HashMap();
HashMap map = new(); // Means the same thing.

Generic Types

ts
let x: Result<User> = getUser();
csharp
Result<User> x = GetUser();

Collection Initialization

ts
let x = ["Bird", "Cat", "Dog"];
let y = [...x];
csharp
string[] x = ["Bird", "Cat", "Dog"];
string[] y = [..x];