0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

3495 snippets · 10 languages
▶ Start learning
C# · BeginnerSnippet

Defining Basic Methods

Methods are blocks of code that perform a specific task. They help organize code and make it reusable. 'void' means the method does not return a value.

snippet.cs
csharp
1
2
3
4
5
void GreetUser(string name) {
Console.WriteLine("Hello, " + name + "!");
}
GreetUser("Alice");
SyntaxFunctions
C# · BeginnerSnippet

Introduction to Classes

A class is a blueprint for objects. In this example, the Book class defines what data a book has, and 'new Book()' creates an actual instance of it.

snippet.cs
csharp
1
2
3
4
5
6
class Book {
public string title;
}
Book myBook = new Book();
myBook.title = "C# Basics";
SyntaxOOP
C# · BeginnerSnippet

Working with Arrays

An array stores a fixed-size collection of elements of the same type. You access elements using an index, starting at 0.

snippet.cs
csharp
1
2
3
string[] inventory = { "Sword", "Shield", "Potion" };
Console.WriteLine(inventory[0]);
Console.WriteLine(inventory.Length);
SyntaxArrays & Lists
C# · BeginnerSnippet

Variables and Data Types

In C#, you must specify the type of data a variable will hold. Common types include string (text), int (whole numbers), and bool (true/false).

snippet.cs
csharp
1
2
3
string playerName = "Alex";
int playerLevel = 5;
bool isOnline = true;
SyntaxData Types
C# · BeginnerSnippet

Conditional Logic with If-Else

The 'if' statement allows your program to make decisions. If the condition in parentheses is true, the first block of code runs; otherwise, the 'else' block runs.

snippet.cs
csharp
1
2
3
4
5
6
7
int score = 85;
if (score >= 50) {
Console.WriteLine("You passed!");
} else {
Console.WriteLine("Try again.");
}
SyntaxControl Flow
C# · BeginnerSnippet

The While Loop

A while loop repeatedly executes a block of code as long as a specified condition remains true. It is useful when the number of iterations is not known in advance.

snippet.cs
csharp
1
2
3
4
5
int counter = 1;
while (counter <= 3) {
Console.WriteLine("Count: " + counter);
counter++;
}
Control FlowSyntax
C# · BeginnerSnippet

Basic Exception Handling

Try-catch blocks are used to handle errors (exceptions) gracefully. The code in the 'try' block runs, and if an error occurs, the 'catch' block handles it instead of crashing the program.

snippet.cs
csharp
1
2
3
4
5
try {
int result = 10 / 0;
} catch (DivideByZeroException ex) {
Console.WriteLine("Error: You cannot divide by zero!");
}
Error HandlingControl Flow
C# · BeginnerSnippet

Reading User Input

Console.ReadLine is used to pause program execution and wait for the user to type something into the terminal. The input is returned as a string.

snippet.cs
csharp
1
2
3
Console.Write("Enter your name: ");
string userName = Console.ReadLine();
Console.WriteLine("Hello, " + userName + "!");
Syntax