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:

22 C# Best Practices

In the ever-evolving world of software development, writing clean and efficient code by using C# best practices is crucial. It not only enhances the maintainability of our codebase but also contributes to the overall success of our projects. In the upcoming sections,...

EF Core Power Tools: Database First in Visual Studio

EF Core Power Tools is a Visual Studio extension that reverse engineers an existing database into a DbContext and entity classes through a graphical interface, so we don't have to write that boilerplate code by hand. It's a popular toolbox for database-first work with...

Memory Optimization With ArrayPool in C#

In this article, we will learn how to achieve memory optimization in C# through the utilization of the ArrayPool class. We'll explore the significance of memory efficiency and validate its effectiveness via benchmarking, providing a comprehensive guide to enhance...

Using a Discard Variable in C#

In this article, we are going to learn about the differences between a discard variable and the usual variables in C#. We will also see some practical uses of a discard.  [sc name="github"...

RandomNumberGenerator in C#: Secure Random Numbers

RandomNumberGenerator is the class in System.Security.Cryptography that produces random values an attacker cannot predict. We use it for keys, tokens, salts and initialisation vectors, and we never use System.Random for any of them. The members we reach for first are...

ReaderWriterLockSlim in C#: When to Use It Over lock

Use ReaderWriterLockSlim instead of lock when many threads read shared data, few threads write it, and each read takes long enough that readers would otherwise queue behind each other. When writes are frequent or the locked code is tiny, a plain lock is simpler and...

Serilog Log Levels and How to Configure Them

Serilog has six log levels, from least to most severe: Verbose, Debug, Information, Warning, Error, and Fatal. Serilog’s own configuration documentation states the default: “if no MinimumLevel is specified, then Information level events and higher will be processed”....

DateTimeOffset vs DateTime in C#: Which to Use?

DateTimeOffset is the one to store. It carries the moment and its offset from UTC, so a value read back in another time zone still identifies the same instant. Microsoft Learn's guidance on choosing between the two says to "consider DateTimeOffset as the default date...

Volatile vs Interlocked vs lock in C#

C# gives us three tools for sharing a variable between threads, and each one solves a different problem. volatile controls the order in which reads and writes become visible. Interlocked makes a read-modify-write on one variable atomic. lock makes a whole block of...

Different Ways to Overwrite a File in C#

In C#, we encounter various scenarios where we might need to overwrite an existing file. Whether to update the file or completely overwrite it, handling the overwrite efficiently is crucial to ensure data integrity and prevent data loss. In this article, we will...

Techniques for Checking Floating-Point Equality in C#

In this article, we will learn several techniques for checking floating-point numbers for equality. If we do not properly understand how floating-point values are stored internally, we might inadvertently write floating-point equality checks that do not behave as...

How to Use Stopwatch in C#

As software engineers, we know system precision and performance are essential, and the ability to measure time can come in handy to achieve this goal. That's where functionalities to capture and analyze time intervals can come in handy. We can leverage this to achieve...

Using HttpContext.Items to Pass Data With ASP.NET Core

In this article, we discuss how to leverage HttpContext.Items to preserve data in a request with ASP.NET Core. We can take advantage of this property to improve the efficiency of our applications. [sc name="github"...

File-Scoped Types in C# 11

The advent of C# 11 has brought with it a host of new features designed to improve and streamline the language's functionality. One of these enhancements is the introduction of file-scoped types, a unique concept designed to prevent name collisions among types,...

How to Use Basic Authentication With HttpClient?

In this article, we are going to discuss how to use basic authentication with HttpClient. While the topic may seem straightforward, there are a few different ways to solve this problem. We'll try and cover some of the main approaches, and hopefully, by the end, we...

Differences Between Any and Exists Methods in C#

In this article, we will explore the differences between Any() and Exists() methods in C#.  Although at first glance they might seem similar, they possess distinct characteristics and we use them in different scenarios in order to manipulate data collections. [sc...

How to Hide an Endpoint From Swagger in ASP.NET Core

Hiding an endpoint means keeping it out of the OpenAPI document our application generates, so it never appears in Swagger UI. The endpoint itself keeps working: every technique below except [NonAction] leaves the route reachable over HTTP. [sc name="github"...