Latest Posts On Code Maze

How to Read a String From a .resx (Resource) File in C#

How to Read a String From a .resx (Resource) File in C#

In this article, we are going to learn how to read a string from a .resx file. While doing this, we are also going to understand what is going on under the hood. Additionally, we will follow some of the best practices on how to manage resource files, and lastly, offer...

Dynamically Adding Rows on a Form in Razor Pages

Dynamically Adding Rows on a Form in Razor Pages

In this article, we will delve into ASP.NET Core forms and how to add rows to them dynamically. We'll use razor pages to create the form and SQLite as our database. [sc name="github"...

AutoMapper: How to Ignore Null Values When Mapping

AutoMapper: How to Ignore Null Values When Mapping

To stop AutoMapper overwriting a destination value with a null from the source, add a condition to the mapping: .ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null)). Members whose source value is null are then skipped, and whatever...

Unit of Work Pattern in C# With EF Core

Unit of Work Pattern in C# With EF Core

The unit of work pattern groups every change a business operation makes and saves them together, in one transaction, so they all succeed or all fail. We will discover its advantages and disadvantages, and build an example implementation with Entity Framework Core....

How and When to Use TPC Inheritance Mapping in EF Core

How and When to Use TPC Inheritance Mapping in EF Core

In this article, we will discuss the recently added Table Per Concrete Type (TPC) inheritance mapping strategy in EF Core 7. In .NET applications, Entity Framework Core (EF Core) has transformed how we communicate with databases by linking object-oriented domain...

ConcurrentQueue in C#: Thread-Safe FIFO Queue

ConcurrentQueue in C#: Thread-Safe FIFO Queue

ConcurrentQueue<T> is a first-in-first-out collection that any number of threads can add to and take from at the same time, without a single lock in our code. It lives in System.Collections.Concurrent, it exposes three methods that matter (Enqueue(),...

C# String Contains Ignore Case: Substring Search

C# String Contains Ignore Case: Substring Search

To find a substring while ignoring case in C#, pass a StringComparison value: source.Contains(needle, StringComparison.OrdinalIgnoreCase). Without that second argument, Contains() is case-sensitive, and there is no other switch that changes it. [sc name="github"...

Replace Line Breaks in a String in C#: ReplaceLineEndings

Replace Line Breaks in a String in C#: ReplaceLineEndings

A line break in C# is not one character. It is any of seven sequences, and ReplaceLineEndings() is the one method that matches all of them: text.ReplaceLineEndings("\n") normalises a mixed string in a single call. String.Replace() and Regex.Replace() also do the job,...

MediatR Exception Handling: IRequestHandler Errors in .NET

MediatR Exception Handling: IRequestHandler Errors in .NET

MediatR handles exceptions through IRequestExceptionHandler<TRequest, TResponse, TException>. We implement it once with open generics, register it in the container, and any exception thrown inside a handler is caught, logged, and turned into a response instead...

Refactoring Dispensables in C#

Refactoring Dispensables in C#

In this article, we will explore refactoring dispensables in C#. We'll focus on identifying code smells and enhancing our code through practical refactoring techniques. [sc name="github"...

C# XPath: Select XML Nodes With SelectSingleNode

C# XPath: Select XML Nodes With SelectSingleNode

XPath is a path language for XML, and .NET exposes it through two methods on XmlNode: SelectSingleNode() returns the first node an expression matches, and SelectNodes() returns every one of them. An expression reads like a file path. /catalog/book walks down from the...

22 C# Best Practices

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

Database First Development With EF Core Power Tools

Database First Development With EF Core Power Tools

In this article, we'll explore EF Core Power Tools, a popular toolbox aimed at simplifying the development workflow when using Entity Framework Core in .NET applications. Its standout feature is the graphical user interface (GUI) it provides for reverse engineering...

Memory Optimization With ArrayPool in C#

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#

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