C# Programming: Learn It Through Ten Short Examples

C# is one of the most popular programming languages in the world.

It is used to build web applications, APIs, desktop software, games, cloud services and mobile apps. It is also a very well designed language, which makes it a good first language and a good long-term career choice.

Instead of reading a long list of keywords, this guide teaches C# through ten short examples. Each one shows an idea you will use in real projects.

C# adds new features with each yearly release alongside .NET, so check Microsoft’s official documentation for the latest version and what it adds.

What Is C#?

C# is a modern, object-oriented, strongly typed language created by Microsoft. It runs on the .NET platform, which works on Windows, Linux and macOS.

Strongly typed means the compiler checks your types, so many mistakes are caught before the program runs.

The Learning Journey

Syntax and Types

↓

Object-Oriented Programming

↓

Collections and LINQ

↓

Modern Language Features

↓

Async Programming

↓

Real-World Projects

Example 1: Variables, Types and Output

string name = "Asha";
int age = 28;
decimal salary = 85000.50m;
var city = "Pune";

Console.WriteLine($"{name}, {age}, lives in {city}");

What it teaches: built-in types, var for type inference and string interpolation with the dollar sign. Use decimal for money, because it avoids the rounding surprises of double.

Example 2: Decisions and Loops

for (int i = 1; i <= 5; i++)
{
    if (i % 2 == 0)
        Console.WriteLine($"{i} is even");
    else
        Console.WriteLine($"{i} is odd");
}

What it teaches: for loops, if and else, and the remainder operator. The same ideas appear in while, foreach and switch.

Example 3: Classes and Objects

public class BankAccount
{
    public string Owner { get; }
    public decimal Balance { get; private set; }

    public BankAccount(string owner, decimal opening)
    {
        Owner = owner;
        Balance = opening;
    }

    public void Deposit(decimal amount)
    {
        if (amount <= 0)
            throw new ArgumentException("Amount must be positive.");
        Balance += amount;
    }
}

What it teaches: encapsulation. Outside code can read the balance but cannot change it directly. Changes go through a method that enforces the rules.

Example 4: Inheritance, Interfaces and Polymorphism

public interface IShape
{
    double Area();
}

public class Circle(double radius) : IShape
{
    public double Area() => Math.PI * radius * radius;
}

public class Rectangle(double width, double height) : IShape
{
    public double Area() => width * height;
}

What it teaches: an interface is a contract. Any class that implements it can be used wherever the interface is expected. This example also uses primary constructors, a feature in recent C# versions that shortens class setup.

Example 5: Collections and LINQ

LINQ lets you query data in a readable way.

var orders = new List<Order>
{
    new(1, "Asha", 120m),
    new(2, "Ravi", 80m),
    new(3, "Asha", 200m)
};

var totals = orders
    .GroupBy(o => o.Customer)
    .Select(g => new { Customer = g.Key, Total = g.Sum(o => o.Amount) })
    .OrderByDescending(x => x.Total);

foreach (var t in totals)
    Console.WriteLine($"{t.Customer}: {t.Total}");

record Order(int Id, string Customer, decimal Amount);

What it teaches: lists, lambda expressions and LINQ operators such as GroupBy, Select and OrderByDescending. The output is Asha: 320 and Ravi: 80. In a program file with top-level statements, type declarations like the record go at the end.

Example 6: Records for Simple Data

The record Order(...) line above is a record, a compact way to define a data type.

Records give you value-based equality, easy copying with the with keyword and a readable text form, with very little code. They are ideal for data that should not change after creation.

Example 7: Pattern Matching

string Describe(object o) => o switch
{
    int n when n > 100 => "big number",
    int => "number",
    string { Length: 0 } => "empty text",
    string s => $"text: {s}",
    null => "nothing",
    _ => "something else"
};

What it teaches: a switch expression that checks type, value and properties in one place. Pattern matching replaces long chains of if statements with clearer code.

Example 8: Null Safety

One of the most common bugs in programming is using something that is null.

string? nickname = GetNickname();
int length = nickname?.Length ?? 0;

What it teaches: nullable reference types tell the compiler which values may be null, and the ?. and ?? operators handle it safely. Turning on nullable checks in a project prevents many runtime errors.

Example 9: Exceptions

try
{
    var text = File.ReadAllText("data.txt");
    Console.WriteLine(text.Length);
}
catch (FileNotFoundException)
{
    Console.WriteLine("File not found.");
}
finally
{
    Console.WriteLine("Done.");
}

What it teaches: handling expected problems without crashing, catching specific exception types, and using finally for cleanup.

Example 10: Async and Await

using var client = new HttpClient();
string json = await client.GetStringAsync("https://example.com/api/items");
Console.WriteLine(json.Length);

What it teaches: waiting for slow work, such as a network call, without blocking the program. In web applications this lets one server handle many requests at the same time.

  •  

Interview Questions to Practice

1. What is the difference between value types and reference types? Value types hold their data directly, and copying creates an independent copy. Reference types hold a reference to an object, so copies share the same object.

2. What is the difference between an interface and an abstract class? An interface defines a contract that many classes can implement. An abstract class can share implementation and state, and a class can inherit from only one.

3. What does the async and await pair do? It lets a method wait for slow operations without blocking the calling thread, which keeps applications responsive and scalable.

4. What is LINQ? Language Integrated Query is a set of methods and syntax for querying and transforming collections and other data sources in a readable way.

5. What is the difference between String and StringBuilder? Strings are immutable, so each change creates a new string. StringBuilder changes a buffer in place, which is more efficient for many modifications.

C# Training at SmartLearnIT

SmartLearnIT’s C# Online Training can help learners build practical skills across C# fundamentals, OOP, LINQ, modern language features, async programming and real application projects.

Training highlights can include:

  • Live instructor-led online classes
  • Real-time doubt resolution
  • Small groups for personal attention
  • Hands-on coding practice and projects
  • Weekly mentorship with industry experts
  • Resume preparation
  • Mock interviews
  • Career support

The focus should be on writing clear, modern C# and understanding why it works.

Explore SmartLearnIT Training

Ready to build practical C# skills?

Course Details: C# Online Training

Contact SmartLearnIT: Contact Us

Watch a Free Technology Training Demo: Technology Training Demo

Explore More Technology Careers: Top 6 Technologies Video

Frequently Asked Questions

1. Is C# good for beginners?

Yes. It is clearly structured, well documented and widely used, and the compiler helps you catch mistakes early.

2. Do I need to know another language first?

No. You can start with C# as your first language.

3. What is the difference between C# and .NET?

C# is the programming language. .NET is the platform that runs C# programs and provides the libraries.

4. What can I build with C#?

Web applications, APIs, desktop software, games with Unity, cloud services and more.

5. Is C# still worth learning in 2026?

Yes. It is actively developed, widely used in enterprises and games, and in steady demand.

6. How long does it take to learn C#?

It depends on your starting point and practice time. With a structured plan and regular coding, many learners become comfortable in about three months.