Lesson 2 of 40
Foundations
Beginner
⏱ 25 min
Variables, Types & Operators
Learn C#'s rich type system — value types, reference types, implicit and explicit casting, string interpolation, and all the operators you need.
Part 1: Built-in Value Types
| Type | Size | Range / Notes |
|---|---|---|
int | 32-bit | –2,147,483,648 to 2,147,483,647 |
long | 64-bit | Very large integers |
double | 64-bit float | ±5.0×10⁻³²⁴ to ±1.7×10³⁰⁸ |
decimal | 128-bit | Precise financial calculations |
bool | 1-bit | true / false |
char | 16-bit | Single Unicode character |
Part 2: var, Implicit & Explicit Casting
var age = 30; // Compiler infers int
var price = 9.99m; // decimal literal
double d = age; // Implicit: int → double ✅
int i = (int)d; // Explicit cast — may lose precision
string s = age.ToString();// Convert int to string
int parsed = int.Parse("42");
var price = 9.99m; // decimal literal
double d = age; // Implicit: int → double ✅
int i = (int)d; // Explicit cast — may lose precision
string s = age.ToString();// Convert int to string
int parsed = int.Parse("42");
Part 3: String Interpolation & Verbatim Strings
string name = "Alice";
int score = 98;
// Interpolated string (preferred)
string msg = $"Player {name} scored {score} points!";
// Verbatim string (no escape sequences)
string path = @"C:UsersAliceDocuments";
// Raw string literals (C# 11+)
string json = """
{ "name": "Alice", "score": 98 }
""";
int score = 98;
// Interpolated string (preferred)
string msg = $"Player {name} scored {score} points!";
// Verbatim string (no escape sequences)
string path = @"C:UsersAliceDocuments";
// Raw string literals (C# 11+)
string json = """
{ "name": "Alice", "score": 98 }
""";
Part 4: Operators Reference
| Category | Operators |
|---|---|
| Arithmetic | + - * / % ++ -- |
| Comparison | == != < > <= >= |
| Logical | && || ! & | |
| Null-handling | ?? ??= ?. ?[] |
| Pattern | is as switch |
| Bitwise | & | ^ ~ << >> |
C# in Visual Studio 2026
📘 This lesson is part of the book C# in Visual Studio 2026 by Dr. Liew Voon Kiong.
View on Amazon Kindle Edition