Learn C# - 10 Code Examples & CST Typing Practice Test
C# (C-Sharp) is a modern, object-oriented, multi-paradigm programming language built by Microsoft for the .NET platform. It is designed for productivity, type safety, performance, and building scalable applications across desktop, web, mobile, gaming, and cloud systems.
Learn CSHARP with Real Code Examples
Updated Nov 17, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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#.
Frequently Asked Questions about C#
What is C#?
C# (C-Sharp) is a modern, object-oriented, multi-paradigm programming language built by Microsoft for the .NET platform. It is designed for productivity, type safety, performance, and building scalable applications across desktop, web, mobile, gaming, and cloud systems.
What are the primary use cases for C#?
Enterprise backend systems. Web APIs (ASP.NET Core). Unity game development. Desktop software (WPF/WinUI). Cloud-native microservices on Azure. Cross-platform mobile apps (MAUI/Xamarin)
What are the strengths of C#?
Excellent tooling and developer productivity. High performance with modern .NET. Strong type safety. Great for enterprise systems. Massive ecosystem (ASP.NET, Unity, MAUI)
What are the limitations of C#?
Heavily tied to Microsoft ecosystem historically. Slightly more complex runtime model. Not ideal for low-level systems. Unity uses older C# versions at times
How can I practice C# typing speed?
CodeSpeedTest offers 10+ real C# code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.