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
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

C# Cheat Sheet — Complete Syntax, OOP Concepts, and Examples

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

ConceptDescriptionExample
File Extension.csProgram.cs
NamespaceOrganizes classesnamespace DemoApp { }
Entry PointMain methodstatic void Main()
CommentsSingle or multi-line// Comment or /* Comment */

Example:

using System;

namespace HelloWorld {
    class Program {
        static void Main() {
            Console.WriteLine("Hello, C#!");
        }
    }
}

Data Types in C#

TypeKeywordSize (bytes)Example
Integerint4int age = 25;
Floating-pointfloat, double, decimal4 / 8 / 16float price = 12.5F;
Characterchar2char grade = 'A';
Booleanbool1bool isValid = true;
StringstringVariablestring name = "John";
ObjectobjectAny typeobject data = 42;

C# Variables and Constants

int x = 10;
const double PI = 3.1416;
var message = "Hello!";
  • var allows type inference at compile time.
  • const defines immutable values.

Operators in C#

CategoryOperatorsExample
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#

TypeSyntaxExample
forfor(init; condition; increment)for(int i=0;i<5;i++)
whilewhile(condition)while(i<10)
do-whiledo { } while(condition);Executes once
foreachforeach(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)

ConceptDefinitionExample
ClassBlueprint for objectsclass Car { }
ObjectInstance of classCar myCar = new Car();
EncapsulationData hiding via access modifiersprivate int age;
InheritanceReusing base classclass Child : Parent
PolymorphismMethod overridingvirtual and override
AbstractionHiding complexityabstract 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

TypeNamespaceExample
ArraySystemint[] nums = {1,2,3};
ListSystem.Collections.GenericList<int> list = new List<int>();
DictionarySystem.Collections.GenericDictionary<int,string> dict = new();
QueueFIFOQueue<string> q = new();
StackLIFOStack<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

KeywordPurpose
publicAccessible from anywhere
privateAccessible only within class
protectedAccessible in derived class
staticBelongs to class, not instance
constConstant value
readonlyAssigned only once
virtualOverridable method
overrideReplaces base method
abstractMust be implemented in child class
interfaceDefines contract
usingImports 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.

Related Article
Machine Learning Cheatsheet (Unsupervised & Reinforcement Learning)

Machine Learning (ML) is a crucial part of artificial intelligence, enabling systems to automatically learn from data.This Machine Learning Cheatsheet Read more

HTML Cheat Sheet — Reference Guide to HTML Tags, Attributes, and Examples

HTML cheat sheet, HTML tags reference, HTML attributes list, HTML examples for beginners, semantic HTML guide, HTML forms tutorial, HTML Read more

Python Cheat Sheet — Complete Syntax Reference and Programming Examples

This Python Cheat Sheet is your quick reference guide for writing efficient Python code. Whether you’re preparing for coding interviews, Read more

PHP Cheat Sheet — Complete Syntax Reference and Programming Examples for Beginners

PHP Cheat Sheet — Complete Syntax Reference & Examples This PHP Cheat Sheet serves as a quick, structured reference for Read more

JavaScript Cheat Sheet — Complete ES6 Syntax, Functions, and DOM Methods with Examples

JavaScript Cheat Sheet — Complete Syntax Reference & Examples JavaScript is the core scripting language of the web, enabling interactivity, Read more

CSS Cheat Sheet — Complete CSS3 Selectors, Properties, and Layout Examples

CSS Cheat Sheet — Complete CSS3 Syntax, Selectors & Layout Examples Cascading Style Sheets (CSS) is the language used to Read more

Java Cheat Sheet — Complete Java Syntax, Data Types, Loops, and OOP Concepts for Beginners

Java Cheat Sheet — Complete Java Syntax, Classes, and Examples Java is a powerful, object-oriented programming language widely used for Read more

HTML5 Cheat Sheet — Complete Tag Reference & Examples

HTML5 is the core markup language of the modern web, used to structure content such as text, images, forms, and Read more

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments