Catalog / C# Cheat Sheet
C# Cheat Sheet
A comprehensive cheat sheet for the C# programming language, covering syntax, data types, control flow, object-oriented programming, and more. This cheat sheet is designed to provide a quick reference for both beginners and experienced C# developers.
Basic Syntax and Data Types
Basic Structure
A basic C# program structure:
|
|
Data Types
int |
32-bit integer. Example: |
float |
32-bit floating-point number. Example: |
double |
64-bit floating-point number. Example: |
bool |
Boolean value (true or false). Example: |
char |
Single Unicode character. Example: |
string |
Sequence of characters. Example: |
decimal |
128-bit data type suitable for financial and monetary calculations. Example: |
Variables
Variable declaration:
Example:
|
Control Flow
Conditional Statements
if Statement |
|
if-else Statement |
|
if-else if-else Statement |
|
switch Statement |
|
Loops
for Loop |
|
while Loop |
|
do-while Loop |
|
foreach Loop |
|
Jump Statements
break |
Terminates the loop or switch statement. |
continue |
Skips the current iteration of the loop and proceeds to the next iteration. |
return |
Exits the current method. |
goto |
Transfers control to a labeled statement (use with caution). |
Object-Oriented Programming
Classes and Objects
Class definition:
Object creation:
|
Encapsulation
Access Modifiers |
|
Properties |
Provide controlled access to class fields.
|
Inheritance
Inheritance allows a class to inherit properties and methods from another class.
|
Example:
|
Polymorphism
Virtual Methods |
Methods that can be overridden in derived classes.
|
Abstract Classes and Methods |
Abstract classes cannot be instantiated and may contain abstract methods (methods without implementation).
|
Interfaces |
Define a contract that classes can implement.
|
Advanced Concepts
Delegates and Events
Delegates are type-safe function pointers. Events provide a way for a class to notify other classes when something of interest happens.
|
Example:
|
LINQ (Language Integrated Query)
LINQ provides a unified way to query data from various sources.
Or using method syntax:
|
Asynchronous Programming
async and await |
Keywords for writing asynchronous code.
|
Task |
Represents an asynchronous operation.
|
Exception Handling
Exception handling using
|