MultipartFormDataContent in C#: Send Multipart Form-Data
MultipartFormDataContent is the HttpContent subclass that builds a multipart/form-data request body in .NET. We add one part per field or file, and it handles the boundary string and the per-part headers for us. Every part is itself an HttpContent. A text field is a...
How to Get Formatted JSON in .Net Using C#
This article will examine how we can get a formatted JSON representation of our C# objects using two popular libraries - Json.NET and System.Text.Json. [sc name="github" url="https://github.com/CodeMazeBlog/CodeMazeGuides/tree/main/json-csharp/HowToGetFormattedJson"...
How to Mock EF Core DbContext
While unit-testing EF Core applications, sometimes, we may want to mock EF Core DbContext. In this article, we'll talk about a few ways of doing that. ...
How to Convert JSON to DataTable in C#
In this article, we are going to discuss various methods that we can use to convert JSON to DataTable in C#. A DataTable is a representation of a database table that we can use to store data as a collection of rows and columns in a grid format. All the methods that we...
.NET Dependency Injection With Constructor Parameters
Introduction In this article, we'll look at ways to use dependency injection with constructor parameters in .NET Core. Dependency injection (DI) with constructor parameters is a way for us to pass the things our class needs (depends on) when we create a new instance...
GetValueOrDefault in C#: Return a Dictionary Default Value
Reading a key a Dictionary<TKey, TValue> does not hold throws a KeyNotFoundException. When a missing key is an ordinary case rather than a bug, GetValueOrDefault() is the method we want: it returns default(TValue) instead of throwing, and its second overload...
Asynchronous Programming Patterns in .NET
In this article, we are going to learn about the various patterns to implement asynchronous programming in .NET. .NET has had in-built support for asynchronous programming since the release of .NET Framework 1.1. It has been through various improvements over the...
How to Copy or Clone a List in C#
Copying a List<T> in C# is one expression: List<string> copy = [.. original]; gives us a new list holding the same elements. The List<T> constructor and LINQ's ToList() do exactly the same thing. [sc name="github"...
How to Insert a Key Value Pair Into an Existing JSON in C#
In this article, we are going to learn how to insert a key-value pair into an existing JSON object. Let’s start. Use Json.NET to...
Refactoring Bloated Code in C#
In this article, we will learn what anti-patterns belong to the group of code smells called bloaters and different techniques for refactoring bloated code in C#. [sc name="github"...
Introduction to Behavior Driven Development (BDD) in C#
Behavior Driven Development, or short, BDD is a software development methodology that relies on examples to define the behavior of a system. In this article, we're going to learn the bases of BDD, see what tools are available, and see BDD in action through simple...
Two Factor Authentication with Web API and Angular using Google Authenticator
In this article, we will enable Two-Factor Authentication with Web API and Angular using Google Authenticator. We can use the Google Authenticator app to get a One-Time Password (OTP) value to enter during login. The initial steps of this article have been taken from...
How to Execute CLI Applications From C#
In this article, we will learn how to execute CLI applications in C# using a built-in class called Process and using open-source libraries. We will cover how we can execute CLI, how we can check if the CLI execution was successful, and how we can react to the...
Method Overriding in C#: override vs new
Method overriding is a derived class replacing a method it inherited. The base class marks the method virtual or abstract to permit it, the derived class marks its own version override, and from then on the object decides which implementation runs, not the variable...
How to Implement Checkbox List in ASP.NET Core
A Checkbox List is an important feature to encourage multiple selections of inputs in forms. We will start with a brief overview of the existing checkbox functionality in ASP.NET Core and later on move to the implementation of a checkbox list. [sc name="github"...
Tracking Application Health With OpenTelemetry Metrics in .NET
The ability to gather metrics for our .NET applications is very important, as it allows us to gain insight into the health and performance, as well as any issues our applications may be experiencing. Metrics are one of the 3 fundamental pillars of OpenTelemetry, and...
JObject in C#: How to Get Value by Key
JObject is Json.NET's in-memory model of a JSON object, and it behaves like a dictionary: jsonObject["price"] hands back the value stored under that key. What comes back is a JToken, never a string or an int, so every read ends in a conversion. [sc...
How to Use SQL LIKE Operator With LINQ in C#
In this article, we will learn how to use the SQL LIKE operator with LINQ in C#. Let's start. Sample Application We will start with a .NET 7Â console...
How to Get an Enum Member as a String in C#
In this article, we're going to learn how to get a string representation of an enum member in C#. The possibility of defining enumeration types in C# - most widely known by the keyword enum - is a very useful feature. We can use them to assign named constants that...
Check If a String Array Contains a Value and Get Index
In this article, we are going to see the different ways we can check for a specific value in a string array and return the index of that value. We will focus mainly on linear search methods. We cannot use binary search methods since we do not know if the array is...
ConcurrentDictionary in C# – Detailed Guide
A concurrent dictionary is a key-value collection several threads can read and write at the same time without a lock statement around it. In .NET that type is ConcurrentDictionary<TKey, TValue>, and it lives in the System.Collections.Concurrent namespace...



