πŸŽ‰ New: Top 75 PHP Interview Questions for 2026 β€” Free for all learners

C# Cheat Sheet β€” Complete Syntax, OOP Concepts, and Examples

P
php Guru
Β· November 24, 2025 Β· 4 min read Β· Updated November 24, 2025
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

πŸ“Œ Key Takeaways

  • C# Cheat Sheet β€” Complete Syntax, OOP Concepts, and Examples
  • C# Cheat Sheet
  • C# Basics for Beginners
  • Data Types in C#
  • C# Variables and Constants
  • Operators in C#
Advertisement

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.

P
php Guru
← Previous Post
C++ Programming Cheat Sheet β€” Complete Syntax, OOP Concepts, and Examples for Beginners
Next Post β†’
SQL Cheatsheet β€” Quick Reference for Beginners and Professionals

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 6   +   2   =