C# Fundamentals - Quick Notes
1. Introduction to C#
● C# is an object-oriented, strongly typed programming language developed by Microsoft.
● Runs on the .NET runtime (CLR).
● Used for desktop, web, mobile, cloud, and game development (Unity).
2. Basic Syntax
● Every program starts with a `Main()` method.
● Statements end with a semicolon `;`.
● Code is organized into namespaces and classes.
3. Data Types
● Value types: int, double, float, bool, char, struct.
● Reference types: string, object, arrays, class, interface.
● Nullable types: int?, double?.
4. Variables & Constants
● Variables must be declared with a type: `int age = 25;`.
● Constants: `const double Pi = 3.14;`.
● Readonly: value assigned once, usually in constructor.
5. Operators
● Arithmetic: +, -, *, /, %.
● Comparison: ==, !=, >, <, >=, <=.
● Logical: &&, ||, !.
● Assignment: =, +=, -=, *=, /=.
6. Control Flow
● Conditional: if, else if, else, switch.
● Loops: for, while, do-while, foreach.
● Break and continue statements control loop execution.
7. Methods
● Defined inside classes. Example: `void SayHello(string name) { }`.
● Can return values (`int Add(int a, int b)`).
● Support optional, named, and params arguments.
8. Object-Oriented Programming
● Classes: blueprints for objects.
● Objects: instances of classes.
● Encapsulation: access modifiers (public, private, protected, internal).
● Inheritance: class Child : Parent.
● Polymorphism: method overriding, interfaces, virtual methods.
● Abstraction: abstract classes and interfaces.
9. Properties & Indexers
● Properties provide controlled access to fields.
● Auto-implemented properties: `public int Age { get; set; }`.
● Indexers allow objects to be indexed like arrays.
10. Exception Handling
● `try { ... } catch(Exception ex) { ... } finally { ... }`.
● Throw exceptions using `throw new Exception("message")`.
● Use specific exception types (e.g., NullReferenceException).
11. Collections & LINQ
● Arrays, Lists, Dictionaries, Queues, Stacks.
● LINQ: query collections with SQL-like syntax.
● Example: `var results = from x in list where x > 5 select x;`.
12. Async & Await
● Asynchronous programming avoids blocking.
● Use `async Task` and `await` with async methods.
● Example: `await httpClient.GetStringAsync(url);`.
13. Useful Features
● Delegates and Events (event-driven programming).
● Lambda expressions: `(x) => x * x`.
● Records for immutable data classes.
● Pattern Matching in switch expressions.
14. Best Practices
● Use meaningful variable and method names.
● Keep methods short and focused.
● Use exceptions for errors, not for control flow.
● Follow SOLID principles in OOP.