How to mock static methods

The need to mock static methods in order to add a unit test is a very common problem. It’s often the case that these static methods are in third-party libraries. There are many utility libraries that are completely made up of static methods. While this makes them very easy to use, it makes them really … Read more

C# – Example of using BlockingCollection

The BlockingCollection class is a blocking concurrent queue. It provides an implementation of the producer-consumer pattern. There are two parts to this pattern: BlockingCollection is thread-safe, which means it’s designed to be used by many threads at once. Here’s an example of using BlockingCollection with one consumer and two producers: Note: BlockingCollection.IsCompleted means the queue … Read more

‘CREATE/ALTER’ must be the first statement in a query batch

Problem When you’re executing a CREATE/ALTER statement to create a procedure/view/function/trigger, you get one of the following errors: ‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch ‘CREATE VIEW’ must be the first statement in a query batch. ‘CREATE FUNCTION’ must be the first statement in a query batch. ‘CREATE TRIGGER’ must be … Read more

Error: chromedriver.exe file does not exist in the current directory

Problem I’m trying to use Selenium with the ChromeDriver and I’m running into the following exception: Unhandled Exception: OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html Solution The simplest solution is to install the Selenium.Chrome.WebDriver package: … Read more

C# – Example of using events

Events are an implementation of the observer pattern and consists of two parts: There are three simple steps to using events, which I’ll show below. 1 – Add an event field The first step is to add an event field to a class with the event keyword and a delegate type (such as the generic … Read more

Multithreaded quicksort in C#

One day I decided to challenge myself by trying to implement multithreaded quicksort. I wanted to see how it would compare to the built-in Array.Sort() method. I came up with two algorithms that were 2-4x faster than Array.Sort(): After continuing to tinker, in attempts to further optimize, I came across the AsParallel().OrderBy() method (PLINQ). After … Read more

How to set a timeout for TcpClient.ConnectAsync()

TcpClient has no direct way to set the connection timeout. It doesn’t have any parameters that allow you to control it, and SendTimeout / ReceiveTimeout don’t apply to the initial connection. The way I control the connection timeout is by awaiting a Task.WhenAny() with TcpClient.ConnectAsync() and Task.Delay(). Task.WhenAny() returns when any of the tasks complete. … Read more

How to update UI from another thread

I often need to be able to run multiple threads and update the UI based on the results. For example, I may need to execute GET requests to 10 different endpoints concurrently, and then report their results in a datagrid as they come back. The problem is you can’t just update the UI from any … Read more

C# – Using custom attributes

Attributes are used to store additional info about a class/method/property. The attributes are read at runtime and used to change the program’s behavior. Here are a few examples of commonly used built-in attributes: In general, you should try to use built-in attributes when possible. When it makes sense, you can create your own custom attribute. … Read more

Could not find an implementation of the query pattern for source type

Problem Any time you try to use a Linq extension method (such as Select() to transform list elements) and you haven’t added ‘using System.Linq’, you’ll get a compiler error like this: Could not find an implementation of the query pattern for source type ‘Your Type’.  ‘Select’ not found. In newer versions, the error looks like … Read more

Objects added to a BindingSource’s list must all be of the same type

Problem When you’re binding a control to a list of objects by adding a BindingList to a BindingSource, you get the following exception: System.InvalidOperationException: Objects added to a BindingSource’s list must all be of the same type. Here’s the code causing this: Solution Instead of adding the BindingList to the BindingSource with Add(), set BindingSource.DataSource … Read more

ReportViewer doesn’t appear in the toolbox in Visual Studio

Problem You’re trying to use the ReportViewer control in Visual Studio but it’s not in the toolbox. Other symptoms: Solution Note: Tested in Visual Studio 2017 and Visual Studio 2019. The exact instructions may have slightly different steps depending on which version of VS you’re using. There are two different problems to solve. First, you … Read more