.NET

C#, dotNet Core, ASP.NET, Entity Framework, testing, architecture, and everything .NET developers need.

File and Directory Operations Recap

What you’ve learned so far

Let’s review what you’ve learned about files and directories in C#. 📄 File Operations Write to file: File.WriteAllText("a.txt", "Hello") Read from file: File.ReadAllText("a.txt") Write mul...

Working with Directories

Creating and managing folders in C#

Folders are called directories in C#. You can create, list, and delete them with a few commands. 📂 Create a Directory 1 Directory.CreateDirectory("myfolder"); 🗂️ List Files and Directories 1 2...

Streams and File I/O: Advanced

More control over file reading and writing

Streams give you fine control over reading and writing data, especially large files. 📤 Writing with FileStream 1 2 3 4 5 using (FileStream fs = new FileStream("data.bin", FileMode.Create)) { ...

File I/O Basics

Reading and writing files in C#

C# makes it easy to work with files: reading, writing, and creating them on your system. 📝 Writing to a File 1 2 3 using System.IO; File.WriteAllText("hello.txt", "Hello, file!"); 📖 Reading fr...

Exception Best Practices

How (and when) to use exceptions in C#

Exceptions are powerful, but you should use them wisely. ✅ Best Practices Use exceptions for exceptional cases only. Don’t use them for normal control flow. Catch only what you ...

Custom Exception Classes

Make your own error types

C# lets you create your own exception classes for specific error types. 🛠️ Defining a Custom Exception 1 2 3 4 class NegativeNumberException : Exception { public NegativeNumberException(strin...

Throwing Exceptions

How to signal errors in your own code

Sometimes, your code needs to tell the user “something went wrong.” You do this by throwing exceptions. 🏗️ Throwing an Exception 1 2 3 4 5 6 void Divide(int a, int b) { if (b == 0) th...

try, catch, and finally Blocks

Handling errors gracefully in C#

C# uses try, catch, and finally blocks to help you handle exceptions (errors) safely. 🛠️ try Block Put code that might fail here. 1 2 3 4 try { int n = int.Parse("oops"); // Will throw an ex...

Exception Handling

Dealing with errors in your program

Programs sometimes encounter errors (exceptions). C# gives you tools to handle them safely. ⚠️ What is an Exception? An error that stops your program, like dividing by zero or reading a missing f...

Type Safety with Generics

How generics prevent bugs

Generics keep your code type-safe, because C# checks that you use the right types. 🚫 Without Generics Using object is flexible, but risky: 1 2 3 4 ArrayList items = new ArrayList(); items.Add("h...