Language Fundamentals
C#: Language Fundamentals C# is a strongly-typed, object-oriented language developed by Microsoft. It runs on the .NET CLR and is the primary language for ASP.N…
C#: Language Fundamentals
C# is a strongly-typed, object-oriented language developed by Microsoft. It runs on the .NET CLR and is the primary language for ASP.NET Core, Unity, and Xamarin/MAUI. Modern C# (10–13) continues to add expressive features like records, pattern matching, and primary constructors.
Types & Variables
// Value types (stored on stack, copied on assignment)
int count = 42;
double pi = 3.14159;
bool isActive = true;
char grade = 'A';
decimal price = 19.99m; // decimal for money — exact, no float errors
// Reference types (stored on heap, shared reference)
string name = "Alice";
int[] scores = { 95, 82, 78 };
object anything = "can be any type";
// Type inference
var list = new List<string>(); // inferred as List<string>
var user = new User("Alice", 30);
// Constants and readonly
const double Pi = 3.14159; // compile-time constant
static readonly int MaxSize = 100; // runtime constant (can be computed)
// Nullable value types
int? nullableInt = null;
double? maybeDouble = GetValue(); // returns null if unavailable
int result = nullableInt ?? 0; // null coalescing
int len = nullableInt?.ToString().Length ?? 0; // null conditional
// String
string msg = $"Hello, {name}! You are {count} years old."; // interpolation
string raw = @"C:\Users\Alice\Documents"; // verbatim — no escape needed
string multiline = """
This is a raw string literal (C# 11)
No escaping needed for "quotes" or \backslashes
""";Control Flow
// if / else
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else
grade = 'C';
// Switch statement (classic)
switch (day)
{
case DayOfWeek.Monday:
case DayOfWeek.Tuesday:
Console.WriteLine("Early week");
break;
default:
Console.WriteLine("Other");
break;
}
// Switch expression (C# 8+) — expression form, returns value
string label = status switch
{
"active" => "Active User",
"inactive" => "Inactive User",
null => "Unknown",
_ => $"Unknown status: {status}",
};
// For / foreach
for (int i = 0; i < 10; i++) { }
foreach (var item in collection) { }
// While / do-while
while (condition) { }
do { } while (condition);
// Pattern matching in if
if (obj is string s && s.Length > 0)
Console.WriteLine(s.ToUpper());
// throw expressions (C# 7+)
var value = input ?? throw new ArgumentNullException(nameof(input));Methods & Parameters
// Basic method
public static int Add(int a, int b) => a + b; // expression body
// Optional parameters
public string Greet(string name, string greeting = "Hello") =>
$"{greeting}, {name}!";
// Named arguments
Greet(greeting: "Hi", name: "Bob");
// Params — variable arguments
public int Sum(params int[] numbers) => numbers.Sum();
Sum(1, 2, 3, 4, 5); // no array needed at call site
// Out parameters
bool success = int.TryParse("42", out int value);
if (success) Console.WriteLine(value);
// Ref and in parameters
void Increment(ref int count) => count++;
void Process(in LargeStruct data) { } // in = readonly ref (no copy, no modify)
// Tuples
(string Name, int Age) GetPerson() => ("Alice", 30);
var (name, age) = GetPerson(); // deconstruct
// Local functions
int Factorial(int n)
{
return n <= 1 ? 1 : n * Inner(n - 1);
int Inner(int x) => x <= 1 ? 1 : x * Inner(x - 1); // local function
}