Basic C# Syntax Cheat Sheet

In C#

C# is a modern, object-oriented programming language developed by Microsoft. It was first introduced in 2000 as part of the .NET framework and has since become one of the most popular programming languages in the world. C# is designed to be simple, powerful, and easy to learn, making it an ideal choice for both beginners and experienced programmers. This cheat sheet lists just the “get started”-syntax. Enjoy!

Cheat Sheet

SyntaxDescription
usingUsed to include namespaces in the program
namespaceUsed to define a namespace
classUsed to define a class
structUsed to define a structure
interfaceUsed to define an interface
enumUsed to define an enumeration
delegateUsed to define a delegate
publicAccess modifier that allows access from any code in the program
privateAccess modifier that allows access only within the same class
protectedAccess modifier that allows access within the same class and derived classes
internalAccess modifier that allows access within the same assembly
staticUsed to define a static member that belongs to the class rather than an instance of the class
readonlyUsed to define a read-only field that can only be set in the constructor
constUsed to define a constant value that cannot be changed
virtualUsed to define a method that can be overridden in a derived class
overrideUsed to override a virtual method in a derived class
abstractUsed to define an abstract class or method that must be implemented in a derived class
sealedUsed to prevent a class from being inherited
newUsed to hide a member of a base class in a derived class
baseUsed to access members of a base class from a derived class
thisUsed to refer to the current instance of a class
try-catchUsed to handle exceptions in the program
finallyUsed to execute code after a try-catch block, regardless of whether an exception was thrown
throwUsed to throw an exception
usingUsed to automatically dispose of resources when they are no longer needed
varUsed to declare a variable with an inferred type
foreachUsed to iterate over a collection of items
if-elseUsed to conditionally execute code
switchUsed to conditionally execute code based on the value of a variable
whileUsed to execute code repeatedly while a condition is true
do-whileUsed to execute code repeatedly until a condition is true
forUsed to execute code repeatedly for a specified number of iterations
continueUsed to skip to the next iteration of a loop
breakUsed to exit a loop
returnUsed to return a value from a method
outUsed to pass a parameter by reference
refUsed to pass a parameter by reference
paramsUsed to pass a variable number of arguments to a method
yieldUsed to return a sequence of values from a method

Reference: C# documentation

#