C# cheat sheet
96 one-line examples for C#. Open it while you code, or print it and keep it near the keyboard. Free, with no signup and no ads.
Variables and Types
| Code | What it does |
|---|---|
int count = 10; | Declare a whole number variable. |
double price = 9.99; | Declare a decimal number variable. |
string name = "Ana"; | Declare a text variable. |
bool isReady = true; | Declare a true or false variable. |
char letter = 'A'; | Declare a single character variable. |
var total = 5; | Let the compiler infer the type. |
const int Max = 100; | Declare a value that cannot change. |
int x = 5, y = 6; | Declare two variables on one line. |
long big = 9000000000L; | Declare a large whole number. |
decimal money = 19.95m; | Declare an exact money value. |
int? age = null; | Declare a number that can be null. |
string? note = null; | Declare a text that can be null. |
Operators and Expressions
| Code | What it does |
|---|---|
int sum = a + b; | Add two numbers. |
int diff = a - b; | Subtract one number from another. |
int product = a * b; | Multiply two numbers. |
int quotient = a / b; | Divide two whole numbers. |
int rest = a % b; | Get the remainder of a division. |
count++; | Add one to a variable. |
count--; | Subtract one from a variable. |
count += 5; | Add five to a variable. |
bool ok = a > b; | Compare two values. |
bool same = a == b; | Check if two values are equal. |
bool both = a && b; | Check if two conditions are true. |
bool either = a || b; | Check if one condition is true. |
Conditions
| Code | What it does |
|---|---|
if (age >= 18) { } | Run code when a condition is true. |
if (x > 0) { } else { } | Run one block or the other. |
else if (x == 0) { } | Test another condition. |
switch (day) { case 1: break; } | Choose a block by value. |
case 2: break; | End a switch case. |
default: break; | Run when no case matches. |
string s = x > 0 ? "yes" : "no"; | Pick a value with a short test. |
if (name != null) { } | Check that a value is not null. |
if (list.Count > 0) { } | Check that a list has items. |
if (!isReady) { } | Run code when a condition is false. |
if (a > 0 && b > 0) { } | Test two conditions together. |
switch (x) { case > 5: break; } | Match a range in a switch. |
Loops
| Code | What it does |
|---|---|
for (int i = 0; i < 10; i++) { } | Repeat code a fixed number of times. |
while (i < 10) { i++; } | Repeat code while a condition is true. |
do { i++; } while (i < 10); | Run code once, then test. |
foreach (var item in list) { } | Visit each item in a collection. |
break; | Exit the loop now. |
continue; | Skip to the next loop step. |
for (int i = 10; i > 0; i--) { } | Count down in a loop. |
foreach (char c in text) { } | Visit each character in a string. |
while (true) { break; } | Loop until a break runs. |
for (int i = 0; i < n; i += 2) { } | Step by two in a loop. |
foreach (var row in grid) { } | Visit each row in a grid. |
int i = 0; while (i < 3) i++; | Write a loop on one line. |
Methods
| Code | What it does |
|---|---|
void SayHi() { } | Declare a method with no return value. |
int Add(int a, int b) => a + b; | Declare a short method with a result. |
SayHi(); | Call a method. |
int sum = Add(2, 3); | Call a method and store the result. |
return sum; | Send a value back from a method. |
static void Main() { } | Declare the program entry point. |
public int GetAge() { return 30; } | Declare a public method. |
private void Reset() { } | Declare a method for this class only. |
void Print(string text = "hi") { } | Give a parameter a default value. |
void Log(params string[] lines) { } | Accept any number of arguments. |
void Show(ref int x) { x++; } | Change the caller variable inside a method. |
void Read(out int x) { x = 1; } | Return a value through a parameter. |
Classes and Objects
| Code | What it does |
|---|---|
class Dog { } | Declare a class. |
Dog d = new Dog(); | Create an object from a class. |
public string Name; | Declare a public field. |
public int Age { get; set; } | Declare a simple property. |
public Dog() { } | Declare a constructor. |
d.Name = "Rex"; | Set a field on an object. |
string n = d.Name; | Read a field from an object. |
class Cat : Animal { } | Make a class inherit from another. |
public override string ToString() => Name; | Replace a base class method. |
public virtual void Speak() { } | Allow a method to be replaced. |
record Point(int X, int Y); | Declare a small data type. |
var p = new Point(1, 2); | Create a record object. |
Collections
| Code | What it does |
|---|---|
var list = new List<int>(); | Create an empty list of numbers. |
list.Add(5); | Add an item to a list. |
int first = list[0]; | Read a list item by index. |
list.Remove(5); | Remove an item from a list. |
int n = list.Count; | Get the number of list items. |
var names = new string[] { "A", "B" }; | Create an array of text. |
var map = new Dictionary<int, string>(); | Create a key and value store. |
map[1] = "one"; | Add or set a dictionary entry. |
string v = map[1]; | Read a dictionary value by key. |
map.ContainsKey(1); | Check if a dictionary key exists. |
list.Sort(); | Sort list items in place. |
list.Clear(); | Remove all list items. |
Strings and Errors
| Code | What it does |
|---|---|
string s = "Hello"; | Create a text value. |
int len = s.Length; | Get the number of characters. |
string up = s.ToUpper(); | Make an uppercase copy. |
string t = s.Trim(); | Remove spaces at both ends. |
bool has = s.Contains("ell"); | Check if text contains a part. |
string[] parts = s.Split(','); | Split text into parts. |
string j = string.Join(",", parts); | Join parts into one text. |
string f = $"Hi {name}"; | Build text with a value inside. |
int n = int.Parse("42"); | Convert text to a number. |
bool ok = int.TryParse(s, out int n); | Convert text safely without an error. |
try { } catch (Exception e) { } | Run code and catch errors. |
finally { } | Run code after try and catch. |