🏠 VisualStudioTutor.com  ·  C# Tutorial Home  ·  C# Lesson 2 of 40
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

TypeSizeRange / Notes
int32-bit–2,147,483,648 to 2,147,483,647
long64-bitVery large integers
double64-bit float±5.0×10⁻³²⁴ to ±1.7×10³⁰⁸
decimal128-bitPrecise financial calculations
bool1-bittrue / false
char16-bitSingle 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");

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 }
"""
;

Part 4: Operators Reference

CategoryOperators
Arithmetic+ - * / % ++ --
Comparison== != < > <= >=
Logical&& || ! & |
Null-handling?? ??= ?. ?[]
Patternis 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