C# – How to use JsonNode to read, write, and modify JSON

When you don’t want to create classes for JSON (de)serialization, one option is to use JsonNode. This allows you work with JSON as a mutable DOM that consists of JsonNode objects (JsonObject, JsonArray, JsonValue). You can use it to read, write, and modify JSON. Here’s an example. Let’s say you have the following JSON that … Read more

ASP.NET Core – API model validation attributes

It’s always a good idea to validate data coming into your web API. There are two steps you can do to guard against invalid data: Here’s an example of using model validation attributes: When a request comes in, the framework does two things: Let’s say you send a request with invalid data (boxOfficeMillions is outside … Read more

C# – Call a constructor from another constructor

To call one constructor from another one, you have to use the constructor chaining syntax, like this: This means when you use the Person(string name) constructor, it’ll first call the Person(string name, string birthDate) constructor. If the constructor is in a base class, use base() instead of this(): Employee subclasses Person. So calling base(name) here … Read more

C# – How to handle nulls with SqlDataReader

SqlDataReader returns a DBNull object when a column is null. This isn’t the same as a C# null. You can check if the column is null by comparing it with DBNull.Value or by using SqlDataReader.IsDBNull(). Here’s an example showing these two ways of checking if a column is null: After checking if the column is … Read more