3/1/24, 1:57 PM C# Cheat Sheet & Quick Reference
QuickRef.ME Stars 4.6k
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";
var num = 999;
https://quickref.me/cs 1/6
3/1/24, 1:57 PM C# Cheat Sheet & Quick Reference
var str = "999";
var bo = false;
Primitive Data Types
Data Type Size Range
int 4 bytes -231 to 231-1
long 8 bytes -263 to 263-1
float 4 bytes 6 to 7 decimal digits
double 8 bytes 15 decimal digits
decimal 16 bytes 28 to 29 decimal digits
char 2 bytes 0 to 65535
bool 1 bit true / false
string 2 bytes per char N/A
Comments
// Single-line comment
/* Multi-line
comment */
// TODO: Adds comment to a task list in Visual Studio
/// Single-line comment used for documentation
/** Multi-line comment
used for documentation **/
Strings
string first = "John";
string last = "Doe";
// 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
char[] chars = new char[10];
chars[0] = 'a';
chars[1] = 'b';
string[] letters = {"A", "B", "C"};
int[] mylist = {100, 200};
bool[] answers = {true, false};
Loops
int[] numbers = {1, 2, 3, 4, 5};
for(int i = 0; i < numbers.Length; i++) {
Console.WriteLine(numbers[i]);
}
foreach(int num in numbers) {
Console.WriteLine(num);
}
https://quickref.me/cs 3/6
3/1/24, 1:57 PM C# Cheat Sheet & Quick Reference
# C# Strings
String concatenation
string first = "John";
string last = "Doe";
string name = first + " " + last;
Console.WriteLine(name); // => John Doe
String interpolation
string first = "John";
string last = "Doe";
string name = $"{first} {last}";
Console.WriteLine(name); // => John Doe
String Members
Member Description
Length A property that returns the length of the string.
Compare() A static method that compares two strings.
Contains() Determines if the string contains a specific substring.
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
// Using property of System.String
string lengthOfString = "How long?";
lengthOfString.Length // => 9
// Using methods of System.String
lengthOfString.Contains("How"); // => true
# Misc
General .NET Terms
Term Definition
A collection of services that are required to execute a given
Runtime
compiled unit of code.
Primarily locates, loads, and managed .NET objects. The CLR
also handles memory management, application hosting,
Common Language Runtime (CLR)
coordination of threads, performaing security checks, and
other low-level details.
Code that compiles and runs on .NET runtime. C#/F#/VB are
Managed code
examples.
Code that compiles straight to machine code and cannot be
directly hosted by the .NET runtime. Contains no free memory
Unmanaged code
management, garbage collection, etc. DLLs created from
C/C++ are examples.
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
Remote Work Revolution Cheatsheet Homebrew Cheatsheet
Quick Reference Quick Reference
PyTorch Cheatsheet Taskset Cheatsheet
Quick Reference Quick Reference
© 2023 QuickRef.ME, All rights reserved.
https://quickref.me/cs 6/6