C# – How to use JsonConverterAttribute

You can use JsonConverterAttribute (from System.Text.Json) to apply a specific JsonConverter to a property. Apply this attribute on a property and specify the JsonConverter type to use, like this: In this example, it’s applying ExpirationDateConverter (a custom JSON converter) to handle the ExpirationDate. For reference, here’s ExpirationDateConverter’s definition: Now serialize the object to JSON: Here’s … Read more

C# – Add or overwrite a value in ConcurrentDictionary

The simplest way to add or overwrite a value in a ConcurrentDictionary is to use the indexer: If the key doesn’t exist, this adds it. If the key exists, this overwrites it. The indexer is thread-safe. The indexer is the simplest way to unconditionally add / overwrite a value. Sometimes you’ll want to use other … Read more

C# – Unit testing code that does File IO

If your code does File IO, such as reading text from a file, then it’s dependent on the file system. This is an external dependency. In order to make the unit tests fast and reliable, you can mock out the external dependencies. To mock out the file system dependency, you can wrap the File IO … 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# – Enum generic type constraint

Here’s how you can use Enum as a generic constraint: Note: Microsoft added this feature in C# 7.3. Whenever you have a generic method, it’s a good idea to use generic type constraints. Without constraints, you would have to implement type checking in the generic method and throw exceptions if an invalid type was used. … Read more

C# – How to create a custom exception

To create a custom exception, create a subclass of the Exception class, like this: Then throw it just like you would any other exception, like this: It’s a good idea to call the base constructor from your constructor and pass in your custom error message. If this exception is unhandled, or if you are logging … Read more

Refactoring the Switch Statement code smell

The Switch Statement code smell refers to using switch statements with a type code to get different behavior or data instead of using subclasses and polymorphism. In general, it looks like this: This switch(typeCode) structure is typically spread throughout many methods. This makes the code difficult to extend, and violates the Open-Closed Principle. This principle … Read more

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# – 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