Learn CSHARP with Real Code Examples
Updated Nov 17, 2025
Code Sample Descriptions
1
C# LINQ and Properties
using System;
using System.Collections.Generic;
using System.Linq;
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
}
class Program {
static void Main() {
var people = new List<Person> {
new Person { Name = "Alice", Age = 25, City = "New York" },
new Person { Name = "Bob", Age = 30, City = "London" },
new Person { Name = "Charlie", Age = 35, City = "New York" }
};
var adults = people
.Where(p => p.Age >= 30)
.OrderBy(p => p.Name)
.Select(p => new { p.Name, p.Age })
.ToList();
foreach (var person in adults) {
Console.WriteLine($"{person.Name} is {person.Age} years old");
}
}
}
Shows modern C# features including LINQ, auto-properties, and string interpolation.
2
C# Basic Class and Object
using System;
public class Car {
public string Model { get; set; }
public int Year { get; set; }
public void ShowInfo() {
Console.WriteLine($"Car: {Model}, Year: {Year}");
}
}
class Program {
static void Main() {
Car car = new Car { Model = "Tesla", Year = 2023 };
car.ShowInfo();
}
}
Defines a class with properties and methods, then creates and uses objects.
3
C# List and ForEach
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
fruits.ForEach(f => Console.WriteLine(f));
}
}
Demonstrates iterating over a list using ForEach and lambda expressions.
4
C# Dictionary Example
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, int> ages = new Dictionary<string, int> {
{"Alice", 25},
{"Bob", 30}
};
foreach (var kvp in ages) {
Console.WriteLine($"{kvp.Key} is {kvp.Value} years old");
}
}
}
Shows using Dictionary to store key-value pairs and iterate over them.
5
C# LINQ Filter and Select
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenSquares = numbers.Where(n => n % 2 == 0).Select(n => n * n).ToList();
evenSquares.ForEach(n => Console.WriteLine(n));
}
}
Filters and projects data from a list using LINQ methods.
6
C# Nullable Types and Null-Coalescing
using System;
class Program {
static void Main() {
int? nullableNumber = null;
int value = nullableNumber ?? 10;
Console.WriteLine(value);
}
}
Demonstrates nullable types and using the ?? operator.
7
C# Async Await Example
using System;
using System.Threading.Tasks;
class Program {
static async Task Main() {
await PrintMessageAsync();
}
static async Task PrintMessageAsync() {
await Task.Delay(1000);
Console.WriteLine("Hello after 1 second!");
}
}
Demonstrates async and await for asynchronous operations.
8
C# String Interpolation
using System;
class Program {
static void Main() {
string name = "Alice";
int age = 25;
Console.WriteLine($"{name} is {age} years old");
}
}
Demonstrates using string interpolation to embed expressions inside strings.
9
C# Custom Class with Constructor
using System;
public class Book {
public string Title { get; set; }
public string Author { get; set; }
public Book(string title, string author) {
Title = title;
Author = author;
}
}
class Program {
static void Main() {
Book book = new Book("1984", "George Orwell");
Console.WriteLine($"{book.Title} by {book.Author}");
}
}
Defines a class with a constructor and uses it to initialize properties.
10
C# Events and Delegates Example
using System;
class Publisher {
public event Action OnChange;
public void RaiseEvent() {
OnChange?.Invoke();
}
}
class Program {
static void Main() {
Publisher p = new Publisher();
p.OnChange += () => Console.WriteLine("Event triggered!");
p.RaiseEvent();
}
}
Demonstrates a simple event and delegate usage in C#.