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# – Serialize to JSON in alphabetical order

There are two ways to serialize an object’s properties to JSON in alphabetical order using System.Text.Json: I’ll show how to do these two options below. Option 1 – Manually alphabetize with JsonPropertyOrder You can specify the exact serialization ordering by using the JsonPropertyOrder attribute. Therefore, to serialize in alphabetical order, first arrange the properties in … Read more

C# – Remove spaces from a string

The simplest way to remove spaces from a string is by using string.Replace(). Here’s an example: This outputs the following. Notice the spaces are removed. string.Replace() is good for removing all occurrences of a specific character (” ” in this case). When you want to remove all whitespace characters (spaces, tabs, newlines, etc..), there are … 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# – Serialize and deserialize a multidimensional array to JSON

System.Text.Json doesn’t support serializing / deserializing multidimensional arrays. When you try, it throws an exception like this – System.NotSupportedException: The type ‘System.Int[,] is not supported. You have three options: In this article, I’ll show an example of how to create a custom JsonConverter that handles multidimensional arrays. In this example, I’ll specifically show how to … Read more

C# – Get subclass properties with reflection

When you use reflection to get properties, you can get just the subclass properties by using BindingFlags.DeclaredOnly (this causes it to exclude inherited properties). Here’s an example: Note: Use GetType() if you have an object. Use typeof() if you have a class. The code outputs just the subclass properties (from the Driver subclass): Get base … 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

C# – Using Channel as an async queue

The Channel class (from System.Threading.Channels) is a non-blocking async queue. It implements the producer-consumer pattern, which has two parts: Compare this with using BlockingCollection, which is a blocking concurrent queue. In this article, I’ll show how to use a Channel. 1 – Create the Channel The first step is to create the Channel object. Here’s … Read more