C# Programming Examples

Key takeaway: Practice C# with 23+ runnable code examples — covering basics, control flow, methods, OOP classes, LINQ, collections, and exception handling — with expected output to verify your code.

C# is a modern, object-oriented programming language developed by Microsoft used for ASP.NET Web applications, game development in Unity, enterprise software, and desktop tools. These programs are ideal for CS students, job seekers, and .NET developers.

Last updated: September 2026 · 23 examples · .NET 8.0/9.0 Compatible

Quick Start

  1. Select a C# example program from the sidebar or mobile menu dropdown
  2. Review the source code and expected console output
  3. Run it live in our online C# compiler or local .NET SDK environment
  4. Modify variables, extend class structures, and experiment with LINQ queries

Who is this for: CS students, .NET developers, Unity game engine learners, and software engineering candidates preparing for C# technical interviews.

Related:Online C# Compiler · C# Tutorial · Java Examples

C# Program to Check Prime Number

Run Online

Determines whether an integer is prime by checking divisibility up to Math.Sqrt(n).

Program (C#)
using System;

class Program
{
    static bool IsPrime(int number)
    {
        if (number <= 1) return false;
        for (int i = 2; i * i <= number; i++)
        {
            if (number % i == 0) return false;
        }
        return true;
    }

    static void Main()
    {
        int n = 29;
        Console.WriteLine($"{n} is prime: {IsPrime(n)}");
    }
}
Expected Output
29 is prime: True

Frequently Asked Questions

You can write, compile, and execute C# code instantly without local setup using our C# Online Compiler. For local development, install the .NET 8.0 SDK and run dotnet run in your terminal.

These examples are fully compatible with .NET 8.0 and .NET 9.0, supporting modern C# features such as top-level statements, string interpolation, pattern matching, tuple deconstruction, and LINQ methods.

C# combines the object-oriented structure of Java with language modernizations like LINQ, properties, async/await, nullable reference types, and native integration with the cross-platform .NET ecosystem.

Yes! All examples are free to copy, modify, and practice. They cover core topics frequently tested in CS college labs and .NET developer interviews.

Who Should Practice These C# Examples?

Target Audience: Developers and students looking to master modern C#:

  • CS & IT Students — university data structures, OOP labs, and .NET coursework
  • ASP.NET Web Developers — mastering backend logic, LINQ queries, and object modeling
  • Unity Game Developers — building gameplay scripts, C# component logic, and OOP patterns
  • Interview Candidates — preparing for .NET C# engineering interviews at tech enterprises

C# & .NET Developer Career Opportunities

C# remains one of the top enterprise programming languages globally, powering web backends, Azure cloud microservices, desktop apps, and Unity games.

Experience LevelAvg Salary (US)Avg Salary (India)Key Roles
Junior (0–2 yrs)$75,000 - $95,000₹4.5 - ₹8 LPAJunior .NET Developer, Unity Programmer
Mid-Level (3–5 yrs)$105,000 - $140,000₹12 - ₹20 LPAC# Backend Engineer, Full-Stack .NET Dev
Senior (6+ yrs)$150,000 - $210,000+₹22 - ₹40+ LPA.NET Solutions Architect, Lead Game Dev

Related Tools:Online C# Compiler · C# Tutorial · Java Examples