DEV Community

Cover image for 🧠 The Ultimate C# Cheat Sheet & Quick Reference
Elmer Chacon
Elmer Chacon

Posted on

🧠 The Ultimate C# Cheat Sheet & Quick Reference

Whether you're new to C# or a seasoned developer who needs a quick refresher, this cheat sheet is for you. It’s a compact but powerful reference filled with practical examples and syntax reminders that’ll keep you sharp and efficient as you code.

🧱 Basics

βœ… Variables

int age = 30;
double pi = 3.14;
string name = "Elmer";
bool isActive = true;
char initial = 'E';
Enter fullscreen mode Exit fullscreen mode

βœ… Constants

const double Gravity = 9.81;
Enter fullscreen mode Exit fullscreen mode

βœ… Nullable types

int? score = null;
Enter fullscreen mode Exit fullscreen mode

πŸ” Conditionals & Loops

βœ… If-Else

if (age > 18)
    Console.WriteLine("Adult");
else
    Console.WriteLine("Minor");
Enter fullscreen mode Exit fullscreen mode

βœ… Switch

switch (dayOfWeek)
{
    case "Monday":
        Console.WriteLine("Start of week");
        break;
    default:
        Console.WriteLine("Any day");
        break;
}
Enter fullscreen mode Exit fullscreen mode

βœ… For loop

for (int i = 0; i < 5; i++)
    Console.WriteLine(i);
Enter fullscreen mode Exit fullscreen mode

βœ… Foreach

string[] fruits = { "Apple", "Banana" };
foreach (string fruit in fruits)
    Console.WriteLine(fruit);
Enter fullscreen mode Exit fullscreen mode

βœ… While

int counter = 0;
while (counter < 3)
{
    Console.WriteLine(counter);
    counter++;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Classes & Objects

βœ… Defining a Class

public class Dog
{
    public string Name { get; set; }
    public void Bark() => Console.WriteLine("Woof!");
}
Enter fullscreen mode Exit fullscreen mode

βœ… Using a Class

var myDog = new Dog { Name = "Rex" };
myDog.Bark();
Enter fullscreen mode Exit fullscreen mode

🎯 Methods

βœ… Method with Parameters and Return

int Add(int a, int b) => a + b;
Enter fullscreen mode Exit fullscreen mode

βœ… Optional Parameters

void Print(string msg = "Hello") => Console.WriteLine(msg);
Enter fullscreen mode Exit fullscreen mode

🧰 Collections

βœ… Arrays

int[] nums = { 1, 2, 3 };
Enter fullscreen mode Exit fullscreen mode

βœ… List

List<string> names = new List<string> { "Alice", "Bob" };
names.Add("Charlie");
Enter fullscreen mode Exit fullscreen mode

βœ… Dictionary

var ages = new Dictionary<string, int> { { "Tom", 30 } };
Console.WriteLine(ages["Tom"]);

Enter fullscreen mode Exit fullscreen mode

πŸš€ LINQ

var evens = numbers.Where(n => n % 2 == 0).ToList();
var names = users.Select(u => u.Name).ToList();
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Exception Handling

try
{
    int x = 10 / 0;
}
catch (DivideByZeroException ex)
{
    Console.WriteLine("Cannot divide by zero");
}
finally
{
    Console.WriteLine("Cleanup code");
}
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Async / Await

async Task<string> GetDataAsync()
{
    await Task.Delay(1000);
    return "Data loaded";
}

Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ Useful Tips

  • Use var when the type is obvious

  • Use ?? for null-coalescing:

string name = input ?? "Default";
Enter fullscreen mode Exit fullscreen mode
  • Use string interpolation:
Console.WriteLine($"Hello, {name}!");
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Bonus: Attributes & Enums

βœ… Enum

enum Status { Pending, Approved, Rejected }
Enter fullscreen mode Exit fullscreen mode

βœ… Attribute

[Obsolete("Use NewMethod instead")]
void OldMethod() {}
Enter fullscreen mode Exit fullscreen mode

🧠 Final Thoughts

This cheat sheet is just the tip of the iceberg when it comes to C#. Keep it handy as a quick reference, especially when you're juggling multiple projects or switching between languages. C# is expressive, powerful, and cleanβ€”and with a reference like this, you can focus more on building and less on remembering syntax.

Top comments (0)