Table of Contents:
C# Cheat Sheet — Quick Reference Guide for Developers
C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft. It runs on the .NET framework and is widely used for building desktop apps, web apps, games, and enterprise software.
This C# Cheatsheet gives you a quick and complete overview of all major topics — from basic syntax to advanced OOP and LINQ — with easy-to-understand examples for practical learning.
C# Basics for Beginners
| Concept | Description | Example |
|---|---|---|
| File Extension | .cs | Program.cs |
| Namespace | Organizes classes | namespace DemoApp { } |
| Entry Point | Main method | static void Main() |
| Comments | Single or multi-line | // Comment or /* Comment */ |
Example:
using System;
namespace HelloWorld {
class Program {
static void Main() {
Console.WriteLine("Hello, C#!");
}
}
}
Data Types in C#
| Type | Keyword | Size (bytes) | Example |
|---|---|---|---|
| Integer | int | 4 | int age = 25; |
| Floating-point | float, double, decimal | 4 / 8 / 16 | float price = 12.5F; |
| Character | char | 2 | char grade = 'A'; |
| Boolean | bool | 1 | bool isValid = true; |
| String | string | Variable | string name = "John"; |
| Object | object | Any type | object data = 42; |
C# Variables and Constants
int x = 10;
const double PI = 3.1416;
var message = "Hello!";
varallows type inference at compile time.constdefines immutable values.
Operators in C#
| Category | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % | x + y |
| Relational | == != > < >= <= | if(a > b) |
| Logical | `&& | |
| Assignment | = += -= *= /= | x += 5; |
| Conditional | ?: | result = (a > b) ? a : b; |
| Null-Coalescing | ?? | value = name ?? "Guest"; |
Conditional Statements in C#
if (x > 0)
Console.WriteLine("Positive");
else if (x < 0)
Console.WriteLine("Negative");
else
Console.WriteLine("Zero");
Switch Example:
switch (day) {
case 1: Console.WriteLine("Monday"); break;
default: Console.WriteLine("Invalid"); break;
}
Loops in C#
| Type | Syntax | Example |
|---|---|---|
| for | for(init; condition; increment) | for(int i=0;i<5;i++) |
| while | while(condition) | while(i<10) |
| do-while | do { } while(condition); | Executes once |
| foreach | foreach(var item in collection) | Loops through arrays or lists |
Example:
foreach (int n in new int[]{1,2,3})
Console.WriteLine(n);
C# Functions (Methods)
int Add(int a, int b) {
return a + b;
}
- Static Methods: Belong to the class, not instance.
- Overloading: Same name, different parameters.
- Default Parameters:
void Greet(string name="User").
C# Object-Oriented Programming (OOP)
| Concept | Definition | Example |
|---|---|---|
| Class | Blueprint for objects | class Car { } |
| Object | Instance of class | Car myCar = new Car(); |
| Encapsulation | Data hiding via access modifiers | private int age; |
| Inheritance | Reusing base class | class Child : Parent |
| Polymorphism | Method overriding | virtual and override |
| Abstraction | Hiding complexity | abstract class Shape {} |
Example:
class Animal {
public virtual void Speak() {
Console.WriteLine("Animal sound");
}
}
class Dog : Animal {
public override void Speak() {
Console.WriteLine("Bark");
}
}
C# Collections
| Type | Namespace | Example |
|---|---|---|
| Array | System | int[] nums = {1,2,3}; |
| List | System.Collections.Generic | List<int> list = new List<int>(); |
| Dictionary | System.Collections.Generic | Dictionary<int,string> dict = new(); |
| Queue | FIFO | Queue<string> q = new(); |
| Stack | LIFO | Stack<int> s = new(); |
c# cheatsheet, c# programming tutorial, c# syntax guide, c# interview questions, c# oops concepts, c# examples for beginners, c# operators, c# loops examples, c# collections, c# reference pdf
LINQ (Language Integrated Query)
LINQ simplifies querying data in collections.
int[] numbers = { 1, 2, 3, 4, 5 };
var even = from n in numbers where n % 2 == 0 select n;
foreach(var n in even)
Console.WriteLine(n);
File Handling in C#
using System.IO;
File.WriteAllText("file.txt", "Hello C#");
string content = File.ReadAllText("file.txt");
Console.WriteLine(content);
Exception Handling in C#
try {
int x = 0;
int y = 10 / x;
}
catch (DivideByZeroException e) {
Console.WriteLine(e.Message);
}
finally {
Console.WriteLine("Cleanup code");
}
Asynchronous Programming in C#
async Task FetchData() {
await Task.Delay(1000);
Console.WriteLine("Data loaded");
}
C# Keywords Summary Table
| Keyword | Purpose |
|---|---|
public | Accessible from anywhere |
private | Accessible only within class |
protected | Accessible in derived class |
static | Belongs to class, not instance |
const | Constant value |
readonly | Assigned only once |
virtual | Overridable method |
override | Replaces base method |
abstract | Must be implemented in child class |
interface | Defines contract |
using | Imports namespaces or disposes resources |
FAQ — C# Programming
Q1: Is C# only used for Windows applications?
No, modern .NET Core allows C# apps to run on Windows, Linux, and macOS.
Q2: What is the difference between C++ and C#?
C++ is a compiled system-level language, while C# runs on the .NET runtime and is designed for managed, high-level development.
Q3: Can I use C# for game development?
Yes, Unity Engine uses C# for all its game scripts.
Q4: Is C# easy to learn for beginners?
Yes! Its syntax is clean, modern, and similar to C, Java, and JavaScript.
Q5: Where can I practice C# online?
Try the PHPOnline C# Compiler for running C# code instantly.

