C# – How to sort by multiple fields

Use OrderBy() and ThenBy() when you want to sort by multiple fields, like this: Note: These are Linq extension methods, so make sure to add a reference to System.Linq. Ascending vs Descending order By default, OrderBy() and ThenBy() sort in ascending order. If you want to sort by descending order, use the Descending version of … Read more C# – How to sort by multiple fields

C# – Use Convert.ChangeType to convert string to any type

You can use Convert.ChangeType() to convert from a string to any type, like this: Normally you’d call the specific type converter method, like Convert.ToBoolean(“true”). However, sometimes it makes sense to use the generalized type converter method – Convert.ChangeType() – instead of hardcoding the calls to specific type converter methods. I’ll show an example of when … Read more C# – Use Convert.ChangeType to convert string to any type

C# – How to unit test async methods

Let’s say you have the following async method you want to test: Here’s how to unit test this: This is awaiting the method you’re testing. To await it, you must make the unit test method return async Task. This example is a bit simplistic. In the real world when you are working with async methods, … Read more C# – How to unit test async methods

C# – Load all types that implement an interface in the current assembly

To get all types in the current assembly that implement a specified interface, use the following: To create instances of these types, loop through them and use Activator.CreateInstance(), like so: Example – Auto-wire a command routing table Let’s say we want to build a command routing table. We have commands and want to automatically wire … Read more C# – Load all types that implement an interface in the current assembly

C# – Generic Plugin Loader

This article explains how to create a generic plugin loader that solves many real world problems when loading plugins in .NET. Besides being generic, this plugin loader also solves the following real world problems when working with plugins: Loads assemblies that have dependencies. Load assemblies that have unmanaged dependencies (like C++ DLLs). Solves the “IsAssignableFrom … Read more C# – Generic Plugin Loader

System.InvalidOperationException: Sequence contains no elements

Problem When you call .First() on an empty IEnumerable, you get the following exception: System.InvalidOperationException: Sequence contains no elements Solution Option 1 – Use .FirstOrDefault() instead of .First() When the IEnumerable is empty, .FirstOrDefault() returns the default value for the type. For reference types this returns null. For value types this returns 0 or that … Read more System.InvalidOperationException: Sequence contains no elements

WinForms: How to handle DataGridViewButtonColumn click event

There’s not a direct way to handle the DataGridViewButtonColumn button click event. Instead, you have to: Handle the DataGridView.CellContentClick event. Check if they clicked the button column. Execute the button click handling logic, passing in the bound data item. In this article I’ll show a step-by-step example of how to handle the button click. Example … Read more WinForms: How to handle DataGridViewButtonColumn click event

C# – Use SemaphoreSlim for throttling threads

When you have multiple threads trying to do work at the same time, and you want to throttle how many of them are actually executing, you can use SemaphoreSlim. Example – a busy grocery store Grocery stores have a limited number of checkout lanes open. Let’s say the grocery store has two lanes open, and … Read more C# – Use SemaphoreSlim for throttling threads

C# – How to use IN with Dapper

Let’s say you have a SQL Query that uses IN and you want to execute it using Dapper. Your query looks something like this: Here’s how you’d execute that with Dapper: Then you’d call it like this: There are two key things to notice about this: You have to exclude the parentheses. I specified one … Read more C# – How to use IN with Dapper