Latest Posts On Code Maze

How to Check if Items of a List Exist in Another List in C#

How to Check if Items of a List Exist in Another List in C#

One of the programming scenarios we can encounter once in a while is checking whether certain items in a list exist in another. This article is going to dive deep into all the techniques to compare lists. We are also going to compare how these techniques perform and...

Using MariaDB With ASP.NET Core Web API

Using MariaDB With ASP.NET Core Web API

In this article, we will discuss how to use a MariaDB database with an ASP.NET Core Web API. First, we will discuss what MariaDB is and how we include it in our application. Then, we will use the Entity Framework Core library to create a mapping between our Web API...

How to Calculate the Number of Days Between Two Dates in C#

How to Calculate the Number of Days Between Two Dates in C#

To get the number of days between two dates in C#, we subtract one DateTime from the other and read the Days property of the TimeSpan we get back. For dates without a time, DateOnly.DayNumber gives the same count, and DateTimeOffset handles dates in different time...

How to Truncate a String to a Maximum Length in C#

How to Truncate a String to a Maximum Length in C#

To truncate a string in C#, we check its length and cut it with Substring() or the range operator only when it's longer than the limit. .NET has no built-in Truncate() method for strings. A common use case is displaying text in user interfaces, such as labels,...

How To Remove HTML Tags From a String in C#

How To Remove HTML Tags From a String in C#

HTML, since its inception, has played a pivotal role in web development, serving as the foundational markup language for creating and structuring web pages. As developers, we engage with web-related data in our programming endeavors, it is a frequent scenario to...

Duende IdentityServer: Add Custom Claims to Access Tokens

Duende IdentityServer: Add Custom Claims to Access Tokens

To add custom claims to a Duende IdentityServer access token, we implement IProfileService and add our claims in GetProfileDataAsync(). That's the supported extension point, and it takes about twenty lines. For user claims that already exist (like roles), there is an...

How to Escape Curly Braces and Special Characters in C#

How to Escape Curly Braces and Special Characters in C#

To escape curly braces in C#, we double them: {{ gives us { and }} gives us }. The rule is the same in string.Format() and in interpolated strings, and raw string literals that start with $$ let us skip the doubling altogether. Curly brackets mark placeholders in a...

Compare Two Lists in C# by a Single Property

Compare Two Lists in C# by a Single Property

To compare two lists on a single property, put the key we are matching on into a HashSet, then filter the other list against it. Two lines, and on our benchmark of 10,000 customers and 500,000 orders it beats every alternative by a wide margin. The reason is what each...

How to Revert a Migration in EF Core

How to Revert a Migration in EF Core

Reverting an EF Core migration usually takes two commands, and the order matters. dotnet ef database update <PreviousMigration> undoes the migration in the database, and dotnet ef migrations remove then deletes its files from the project. Running them the other...

Plugin Architecture Pattern in C#

Plugin Architecture Pattern in C#

In this article, we explore the concept and implementation of a plugin architecture in C# and .NET 8. We also learn about the motivations to use this architecture, as well as the advantages and disadvantages of doing so. [sc name="github"...

How to Get the Temp Folder and Create a Temp File in C#

How to Get the Temp Folder and Create a Temp File in C#

To create a temp file in C#, we call Path.GetTempFileName(). It creates an empty file with a unique name in the temp folder and returns the full path. When we want to choose the file name ourselves, we get the folder with Path.GetTempPath() and combine it with our...

How to Ensure a String Is Valid JSON in C#

How to Ensure a String Is Valid JSON in C#

In this article, we delve into the different approaches to ensure a string is valid JSON in C# by providing detailed explanations and practical examples. JavaScript Object Notation, more commonly known as JSON, has become the de facto data format for interchange...

How to Save a List to a Text File in C#

How to Save a List to a Text File in C#

In this article, we will delve into the methods to save a list to a text file using two C# classes: StreamWriter and File. We will also discuss which class best aligns with the specific requirements for file operations. [sc name="github"...

How to Escape the Backslash Character in C#

How to Escape the Backslash Character in C#

In this article, we will explore different ways to escape the backslash (\) character in C#. By the end, we will have a solid understanding of the best practices for representing literal backslashes in C#, ensuring accurate representations, and preventing unintended...

How to Use IExceptionHandler to Handle Exceptions in .NET

How to Use IExceptionHandler to Handle Exceptions in .NET

Effective error handling plays a crucial role in shaping the behavior of our applications. It involves establishing consistent response formats for our applications, ensuring a standardized approach even when errors occur. .NET 8 comes with the IExceptionHandler...

When to Use Thread.Sleep, When to Use Task.Delay?

When to Use Thread.Sleep, When to Use Task.Delay?

Task.Delay() is the right choice in almost all modern C# code: it waits asynchronously without blocking the thread, so the thread pool keeps working while our code pauses. Thread.Sleep() blocks the calling thread entirely. It's acceptable only in console tools, tests,...

Blazor Sections: SectionOutlet and SectionContent

Blazor Sections: SectionOutlet and SectionContent

Blazor sections let a page push content into a placeholder somewhere else in the layout. SectionOutlet marks the placeholder, usually in MainLayout.razor, and SectionContent on any page or component fills it, with a matching SectionName connecting the two. They were...

How to Set an Authenticator for a New RestClient in RestSharp

How to Set an Authenticator for a New RestClient in RestSharp

In this article, we will review the different authenticator types in RestSharp, available via NuGet. First, we will briefly look through all of them and later focus on setting up a request using the HTTP basic authenticator. Then we will take a look at a common error...