C# Cheat Sheet & Quick Reference
C# Cheat Sheet & Quick Reference
C#
C# quick reference cheat sheet that provides basic syntax and methods.
# Getting Started
Hello.cs
class Hello {
// main method
static void Main(string[] args)
{
// Output: Hello, world!
Console.WriteLine("Hello, world!");
}
}
Compiling and running (make sure you are in the project directory)
$ dotnet run
Hello, world!
Variables
int intNum = 9;
long longNum = 9999999;
float floatNum = 9.99F;
double doubleNum = 99.999;
decimal decimalNum = 99.9999M;
char letter = 'D';
bool @bool = true;
string site = "quickref.me";
https://quickref.me/cs 1/6
3/1/24, 1:57 PM C# Cheat Sheet & Quick Reference
Comments
// Single-line comment
/* Multi-line
comment */
Strings
// string concatenation
string name = first + " " + last;
Console.WriteLine(name); // => John Doe
https://quickref.me/cs 2/6
3/1/24, 1:57 PM C# Cheat Sheet & Quick Reference
User Input
Console.WriteLine("Enter number:");
if(int.TryParse(Console.ReadLine(),out int input))
{
// Input validated
Console.WriteLine($"You entered {input}");
}
Conditionals
int j = 10;
if (j == 10) {
Console.WriteLine("I get printed");
} else if (j > 10) {
Console.WriteLine("I don't");
} else {
Console.WriteLine("I also don't");
}
Arrays
Loops
https://quickref.me/cs 3/6
3/1/24, 1:57 PM C# Cheat Sheet & Quick Reference
# C# Strings
String concatenation
String interpolation
String Members
Member Description
Equals() Determines if the two strings have the same character data.
Format() Formats a string via the {0} notation and by using other primitives.
Removes all instances of specific characters from trailing and leading characters.
Trim()
Defaults to removing leading and trailing spaces.
Removes the provided character and creates an array out of the remaining characters
Split()
on either side.
Verbatim strings
string longString = @"I can type any characters in here !#@$%^&*()__+ '' \n \t
except double quotes and I will be taken literally. I even work with multiple
lines.";
Member Example
https://quickref.me/cs 4/6
3/1/24, 1:57 PM C# Cheat Sheet & Quick Reference
# Misc
General .NET Terms
Term Definition
Related Cheatsheet
Java Cheatsheet
Quick Reference
Recent Cheatsheet
https://quickref.me/cs 5/6
3/1/24, 1:57 PM C# Cheat Sheet & Quick Reference
https://quickref.me/cs 6/6