C# – Get all files in a folder

There are two simple ways to get all files in a folder: I’ll show examples below, along with a few other scenarios, such as getting files from subfolders. Note: ‘directory’ and ‘folder’ mean the same thing. I use these terms interchangeably. Get all files with Directory.GetFiles() Directory.GetFiles() returns all file paths from the top-level folder … Read more

C# – Remove items from a list

Here are the different ways to remove items from a list: I’ll show examples of using these methods. Remove item by index with List.RemoveAt() You can use List.RemoveAt() to remove an item at an index (0-based). Here’s an example of removing the first and last item from the list: This removes the first item “A” … Read more

C# – Using a list of tuples

Here’s an example of how to initialize a list of named tuples: This creates a list of named value tuples (ValueTuple<string, int>) using list initializer syntax and then loops through the tuples. I suggest always using named value tuples (not System.Tuple). This outputs the following: I’ll now show how to add to the list of … Read more

C# – Convert a list to a dictionary

The simplest way to convert a list to a dictionary is to use the Linq ToDictionary() method: This loops through the list and uses the key/element selector lambdas you passed in to build the dictionary. In this article, I’ll go into details about how to use ToDictionary() and show how to deal with duplicate keys. … Read more

Moq – Capture parameters with Callback()

When you’re using Moq to set up a mocked method, you can use Callback() to capture the parameters passed into the mocked method: There are two main use cases for capturing parameters in a test: In this article, I’ll show examples of using Callback() in those two scenarios, and then I’ll explain some problems to … Read more