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

Git – How to change filename casing

When you rename a file by changing its casing on Windows (ex: movie.cs -> Movie.cs), git will ignore the change. To change the casing, use the git mv command: This will rename the movie.cs file to Movie.cs on the file system and in the git repository, resulting in a rename change, which you can then … Read more

C# – Using reflection to get properties

You can get a list of a type’s properties using reflection, like this: Note: If you have an object, use movie.GetType().GetProperties() instead. This outputs the following: When you use GetProperties(), it returns a list of PropertyInfo objects. This gives you access the property’s definition (name, type, etc…) and allows you to get and modify its … Read more

Comparing performance with Benchmark.NET graphs

The following graph compares the execution time of three sort implementations ran against varying input sizes (1k, 10k, 100k): This graph was generated using Benchmark.NET, which I’ll show how to use in this article. I’ll be comparing the performance of multithreaded quicksort implementations (with the non-threaded Array.Sort() as a baseline). Create console app and reference … Read more

HackerRank – Mark and Toys solution

In this article, I’ll explain how to solve the Mark and Toys algorithm problem on HackerRank. Problem statement: Given a fixed budget and a list of item prices. What is the max number of items you can purchase? You can only buy each item once. Note: This is the Mark and Toys problem from HackerRank. … Read more

C# – Circuit breaker with Polly

In an electrical system, a circuit breaker detects electrical problems and opens the circuit, which blocks electricity from flowing. To get electricity flowing again, you have to close the circuit. The same approach can be implemented in software when you’re sending requests to an external service. This is especially important when you’re sending lots of … Read more

C# – Global exception event handlers

There are two global exception events available in all .NET applications: You wire up these event handlers in Main() (before anything else has executed), like this: Note: If you’re using top-level statements, put these statements at the top of the ‘entry point’ file. This outputs the following before crashing: Notice the FirstChanceException event fired first. … Read more

C# – How to load assemblies at runtime using Microsoft Extensibility Framework (MEF)

You can use Microsoft Extensibility Framework (MEF) to load assemblies at runtime. This is an alternative to implementing dynamic assembly loading with a more manual approach (like using AssemblyLoadContext). Here’s an example of using MEF to load an instance of IMessageProcessorPlugin from some assembly located in the C:\Plugins directory: MEF looks for exported types in … Read more