Code Maze Author

Code Maze

This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.
Also find me here:



MY ARTICLES:

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

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...

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#

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#

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#

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...

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,...

How to Append or Update Query String Values in C#

In web development, query strings facilitate communication between clients and servers. A query string is a part of a URL containing data parameters that transmit information from one page to another.Β In C# development, effectively appending and updating query string...

Client-Server Architectural Pattern in C#

The Client-Server Architectural Pattern is a system that divides an application between clients that initiate requests and the server that fulfills them. This model of architecture forms the basis of countless applications, from web browsing to email. In this article,...

Differences Between NUnit, xUnit and MSTest

A testing framework provides us with an approach to automating the validation of the functionality and performance of our code. In this article, we will look at the three major testing frameworks (NUnit, xUnit, and MSTest) that are available for a .NET project.Β ...

How to Initialize Parameters of a Record in C#

In this article, we are going to learn how to initialize the parameters of a record type in C#. For an overview of what records are, have a look at this article. [sc name="github"...

Decimal Precision in C#: Rounding, 28-29 Digits

A C# decimal stores a number as a 96-bit whole number plus a scale that says where the decimal point goes. That is why it counts in tenths and hundredths the way we do, and why 0.1m + 0.2m is exactly 0.3m while the same sum in double is not. [sc name="github"...

Default Values for Lambda Expressions in C#

Microsoft released the newest version of the C# language, C#12. This new release has fresh improvements that make C# even better for developers, such as default values for lambda expressions. To become familiar with this change, let's dive into the topic and learn its...

Implement Custom Authorization Policy Provider in ASP.NET Core

Authorization policies play a major role in modern ASP.NET Core authorization. We generally register authorization policies by giving them a name and specifying their requirements while configuring our startup application. We've covered how to do that in the previous...

JsonPropertyName in C#: How to Retrieve JSON Property Names

To retrieve JSON property names in C#, we read the name attribute on each property. In System.Text.Json that attribute is [JsonPropertyName], in Newtonsoft.Json it is [JsonProperty], and we can read either one through reflection or ask the serializer directly....

How to Serialize an Object into Query String Format in C#

In this article, we will learn how to serialize an object into query string format using C#. We will discuss the challenges we face when dealing with nested objects and arrays and how to deal with them. [sc name="github"...

Debugging and Profiling Multithreaded .NET Applications

In this article, we'll dive into a comprehensive guide about the various tools available in Visual Studio for the debugging and profiling of multithreaded .NET applications. Multithreading, that sounds scary. Well, not really. [sc name="github"...

Querying MongoDB With ObjectId in C#

In this article, we will look at how to retrieve MongoDB documents using ObjectId in C#. We will only cover how to query a MongoDB database using ObjectId in C#. Check out our article Getting Started with ASP.NET Core and MongoDB for more information on how to set up...