Introduction to C# & .NET Setup
What is C#?
C# (pronounced "C-sharp") is a modern, object-oriented, type-safe programming language developed by Microsoft. It is part of the .NET ecosystem and is widely used for building Windows desktop applications, web services, games (Unity), cloud applications, and mobile apps.
Why C#?
- Strongly typed & safe: Catches many errors at compile time.
- Modern features: Async/await, LINQ, records, pattern matching.
- Cross-platform: Runs on Windows, macOS, Linux via .NET 8+.
- Excellent tooling: Visual Studio, VS Code, Rider.
Installing .NET SDK
Download the latest .NET 8 SDK from dotnet.microsoft.com.
dotnet --version
# Expected: 8.0.x or higher
Your First C# Program
using System; class Program { static void Main() { Console.WriteLine("Hello, World!"); Console.WriteLine("Welcome to C# Mastery!"); } }
Welcome to C# Mastery!
Create a new console project and modify it to print your name, age, and favorite programming language.
Operators & Expressions
Arithmetic Operators
C# supports standard arithmetic: +, -, *, /, and % (modulus).
int a = 10, b = 3; Console.WriteLine(a + b); // 13 Console.WriteLine(a / b); // 3 (Integer division) Console.WriteLine(a % b); // 1
Comparison & Logical Operators
- Comparison:
==,!=,>,<,>=,<= - Logical:
&&(AND),||(OR),!(NOT)
Calculate the area of a circle using Math.PI and operators. Take the radius as an input.
Control Flow: If & Switch
If-Else Statements
int temp = 25; if (temp > 30) { Console.WriteLine("Hot"); } else if (temp > 20) { Console.WriteLine("Warm"); } else { Console.WriteLine("Cold"); }
Switch Expressions (Modern C#)
Modern C# uses a concise switch expression syntax.
string result = day switch { 1 => "Monday", 2 => "Tuesday", _ => "Unknown" };
Loops: for, while, foreach
Iterating with Loops
// Standard for loop for (int i = 0; i < 5; i++) { Console.WriteLine(i); } // Foreach loop (best for collections) string[] names = { "Ana", "Felipe", "Emilia" }; foreach (var name in names) { Console.WriteLine($"Hello {name}"); }
Methods & Parameters
Defining Methods
static int Add(int x, int y) => x + y; void Greet(string name, string title = "Dr.") { Console.WriteLine($"Hello {title} {name}"); }
Collections & LINQ
Working with Lists
var numbers = new List<int> { 10, 20, 30, 40 }; numbers.Add(50); numbers.RemoveAt(0);
Introduction to LINQ
LINQ (Language Integrated Query) allows you to query collections like SQL.
var highScores = numbers.Where(n => n > 25).OrderByDescending(n => n);
Strings & Formatting
String Manipulation
string text = " C# Programming "; Console.WriteLine(text.Trim().ToUpper()); Console.WriteLine(text.Contains("C#"));
String Interpolation
Console.WriteLine($"Date: {DateTime.Now:d}"); Console.WriteLine($"Price: {19.99:C}"); // Currency
Object-Oriented Programming
Classes & Objects
public class Person { public string Name { get; set; } public virtual void Work() => Console.WriteLine("Working..."); } public class Developer : Person { public override void Work() => Console.WriteLine("Coding..."); }
Exceptions & File I/O
Handling Errors
try { int x = int.Parse(Console.ReadLine()); } catch (FormatException ex) { Console.WriteLine("Invalid number format."); } finally { Console.WriteLine("Done."); }
Reading/Writing Files
File.WriteAllText("log.txt", "Hello C#"); string content = File.ReadAllText("log.txt");
The .NET Ecosystem
NuGet & Libraries
Use dotnet add package to install community libraries like Newtonsoft.Json.
Asynchronous Programming
public async Task<string> GetDataAsync() { var client = new HttpClient(); return await client.GetStringAsync("https://api.example.com"); }
Capstone Project: Task Manager
Build a console-based Task Manager that uses everything we've learned: Lists, LINQ, OOP, and File I/O.
// Project requirements: // 1. Add/Remove tasks // 2. Mark tasks as complete // 3. Save tasks to tasks.json // 4. Filter tasks using LINQ