C# Programming
Comprehensive Technical Reference
Mastering the .NET Ecosystem
Language Version: C# 12.0
Page 1
Page 1: Introduction to C# & .NET
C# (C-Sharp) is a modern, object-oriented, and type-safe programming language developed by
Microsoft. It runs on the .NET framework.
Key Features:
• Component-Oriented: Built to create modular software.
• Garbage Collection: Automatic memory management.
• Strong Typing: Prevents type errors at compile time.
using System;
namespace HelloWorld {
class Program {
static void Main(string[] args) {
[Link]("Hello, .NET!");
}
}
}
Page 2
Page 2: Variables & Basic Data Types
C# is a statically-typed language, meaning variables must be declared with a type.
• Value Types: stored in the stack (int, float, bool, char, struct).
• Reference Types: stored in the heap (string, class, interface, delegate).
int myNum = 5; // Integer
double myDouble = 5.99D; // Floating point
char myLetter = 'D'; // Character
bool myBool = true; // Boolean
string myText = "Hello"; // String
The var keyword allows for implicit typing where the compiler infers the type.
Page 3
Page 3: Operators & Expressions
C# supports a wide range of operators for mathematical and logical operations.
Common Operators:
• Arithmetic: +, -, *, /, %
• Assignment: =, +=, -=
• Comparison: ==, !=, >, <, >=, <=
• Logical: && (And), || (Or), ! (Not)
int x = 10;
int y = 20;
bool result = (x < y) && (y > 15); // Returns true
Page 4
Page 4: Control Flow Structures
Decision Making
if (condition) {
// code to execute
} else if (otherCondition) {
// code
} else {
// code
}
Switch Statement
Modern C# supports powerful switch expressions.
int day = 4;
string result = day switch {
1 => "Monday",
2 => "Tuesday",
_ => "Unknown"
};
Page 5
Page 5: Iteration (Loops)
Loops allow you to repeat a block of code multiple times.
• For Loop: Use when you know exactly how many times to iterate.
• Foreach Loop: Used specifically to iterate through elements in a collection.
• While Loop: Continues as long as a condition is true.
string[] cars = {"Volvo", "BMW", "Ford"};
foreach (string i in cars) {
[Link](i);
}
Page 6
Page 6: Methods & Parameters
Methods are blocks of code that perform a specific task and only run when called.
static void MyMethod(string name, int age = 20) {
[Link](name + " is " + age);
}
Parameter Modifiers:
• ref: Passes an argument by reference.
• out: Used to return multiple values from a method.
• params: Allows a variable number of arguments.
Page 7
Page 7: Object-Oriented Programming (OOP)
C# is deeply rooted in OOP principles.
Classes and Objects
class Car {
public string model;
public void FullThrottle() {
[Link]("The car is going fast!");
}
}
Inheritance
class ElectricCar : Car {
public int batteryLife;
}
Page 8
Page 8: Properties & Encapsulation
Encapsulation ensures that sensitive data is hidden from users. This is achieved through
Properties.
class Person {
public string Name { get; set; } // Auto-implemented property
private int _age;
public int Age {
get { return _age; }
set {
if (value > 0) _age = value;
}
}
}
Page 9
Page 9: Arrays & Collections
C# provides various ways to store groups of objects.
• Arrays: Fixed size, declared with [].
• Lists: Dynamic size, found in [Link].
• Dictionaries: Store Key-Value pairs.
List<string> names = new List<string>();
[Link]("Alice");
[Link]("Bob");
Page 10
Page 10: Exception Handling & LINQ
Try-Catch
try {
int[] myNumbers = {1, 2, 3};
[Link](myNumbers[10]);
} catch (Exception e) {
[Link]([Link]);
} finally {
[Link]("The 'try catch' is finished.");
}
LINQ (Language Integrated Query)
LINQ allows you to query collections using a syntax similar to SQL.
var scores = new[] { 97, 92, 81, 60 };
var highScores = [Link](s => s > 80).OrderByDescending(s => s);
Page 11