Set Operations – Intersect, Union, Difference, and Symmetric Difference

In the show House, different sets of main characters appeared in different seasons. For example, here are the main characters that starred in seasons 1 and 7: Characters in Season 1 Characters in Season 7 HouseCuddyWilsonForemanChaseCameron HouseCuddyWilsonForemanChaseTaubThirteenMasters Set operations allow you to answer interesting questions, such as “Which House main characters appeared in both seasons … Read more Set Operations – Intersect, Union, Difference, and Symmetric Difference

TDD by Example: How to add tests against code that doesn’t exist yet

I personally use Test-Driven Development (TDD) on a daily basis because it allows me to focus on solving one test case at a time in the simplest way possible, and then refactor with the safety net of unit tests. It’s a bottom-up approach, and is a natural fit for my coding principles (unit test, refactor, … Read more TDD by Example: How to add tests against code that doesn’t exist yet

C# – Hex string to byte array

This article shows code for converting a hex string to a byte array, unit tests, and a speed comparison. First, this diagram shows the algorithm for converting a hex string to a byte array. To convert a hex string to a byte array, you need to loop through the hex string and convert two characters … Read more C# – Hex string to byte array

C# – Use Assert.ThrowsException instead of ExpectedException attribute

If you’re using the built-in MSTest unit testing framework, you may be used to the old way of asserting exceptions by using the [ExpectedException] attribute. The following example illustrates one of the key problems with this approach: The ArgumentNullException is being thrown, so the test passes. However, the exception is being thrown in the arrange … Read more C# – Use Assert.ThrowsException instead of ExpectedException attribute

ASP.NET – How to use a BackgroundService for long-running and periodic tasks

In ASP.NET, when you need a long-running background task, or need to run a task periodically, you can implement IHostedService or BackgroundService and register your class as a hosted service in Startup. As you may have already encountered, if you try to run a long-running background task, the web server will randomly kill your task … Read more ASP.NET – How to use a BackgroundService for long-running and periodic tasks

C# – How to unit test code that uses HttpClient

HttpClient is a dependency. Like any other dependency, you need to pass it into your code (aka dependency injection). By passing it in, you can mock it out in unit tests. There are two approaches to mocking it out: Wrap the HttpClient and mock out the wrapper. Use a real HttpClient with a mocked out … Read more C# – How to unit test code that uses HttpClient

C# – Parameterized tests with MSTest v2

Here’s an example of how to parameterize your tests using the built-in MSTest v2 test framework: There are 3 steps: Add parameters to your test method. Use [DataTestMethod] instead of [TestMethod]. For each test case, add [DataRow(…)] to pass in the parameters for that test case. What parameters can you pass in? You pass in … Read more C# – Parameterized tests with MSTest v2

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

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