A basic C# program structure:
using System;
namespace MyNamespace
{
class MyClass
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
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.
A basic C# program structure:
|
|
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: |
Variable declaration:
Example:
|
if Statement |
|
if-else Statement |
|
if-else if-else Statement |
|
switch Statement |
|
for Loop |
|
while Loop |
|
do-while Loop |
|
foreach Loop |
|
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). |
Class definition:
Object creation:
|
Access Modifiers |
|
Properties |
Provide controlled access to class fields.
|
Inheritance allows a class to inherit properties and methods from another class.
|
Example:
|
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.
|
Delegates are type-safe function pointers. Events provide a way for a class to notify other classes when something of interest happens.
|
Example:
|
LINQ provides a unified way to query data from various sources.
Or using method syntax:
|
async and await |
Keywords for writing asynchronous code.
|
Task |
Represents an asynchronous operation.
|
Exception handling using
|