C# – ‘internal’ vs ‘protected’

The public/private access modifiers are straightforward: public means everything has access while private means only the class has access. The internal/protected access modifiers are a little more complicated. In other words, internal means it’s “private” to the assembly and protected means it’s “private” to the class and its subclasses. To illustrate the difference, I’ll show … Read more

ASP.NET Core – Control the graceful shutdown time for background services

ASP.NET Core gives you a chance to gracefully shutdown your background services (such as cleaning up and/or finishing in-progress work). By default, you’re given a grand total of 5 seconds to shutdown all background services. If you need more time than this, there are a few ways to control the graceful shutdown time, which I’ll … Read more

C# – Get subclass properties with reflection

When you use reflection to get properties, you can get just the subclass properties by using BindingFlags.DeclaredOnly (this causes it to exclude inherited properties). Here’s an example: Note: Use GetType() if you have an object. Use typeof() if you have a class. The code outputs just the subclass properties (from the Driver subclass): Get base … Read more

C# – Deserialize JSON to a derived type

The simplest way to deserialize JSON to a derived type is to put the type name in the JSON string. Then during deserialization, match the type name property against a set of known derived types and deserialize to the target type. System.Text.Json doesn’t have this functionality out of the box. That’s because there’s a known … Read more

C# – Property order with System.Text.Json

You can use the JsonPropertyOrder attribute to control the order that properties get serialized. You specify the order as an integer, and it serializes the properties in ascending order. Here’s an example: Note: Properties have a default order value of 0. Now serialize an object to JSON: This generates the following JSON: Notice the properties … Read more

EF Core – Inheritance mapping

There are two ways to do inheritance mapping in EF Core: Let’s say we have a database with employees. All employees have an id and a name. There are currently two types of employees: programmers and drivers. Programmers have a language (ex: C#), and drivers have a car (ex: Honda). We can model this with … Read more

C# – Access modifiers

Access modifiers are used to hide class members (methods/properties/fields) from other code. When you define a class/method/property/field, you put an access modifier on it. In C#, there are four main access modifiers: Access modifiers are enforced at compile time. When you try to use a class member that you can’t access, you get the CS0122 … Read more

C# – Default method implementations and constants in interfaces

You can add default method implementations (and constants) to interfaces (in C# 7+), like this: Note: The Log() method is converting the DateTime to a string with a custom format and including it with the logging message. This outputs the following: Microsoft’s stated purpose for this feature is that it allows you add methods to … 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