Latest Posts On Code Maze

ModuleInitializer Attribute in C#

ModuleInitializer Attribute in C#

As a C# developer, you might have overlooked the ModuleInitializer attribute introduced in version 9 of the language. This attribute is a hidden gem in the extensive BCL APIs and designates a method that the runtime should first invoke when loading the assembly....

IdentityUser in ASP.NET Core: Add Custom Properties

IdentityUser in ASP.NET Core: Add Custom Properties

IdentityUser gives us a user with a name, an email, a password hash and a lockout state, and no place to put a column of our own. The fix is to subclass it: ApplicationUser : IdentityUser, our properties on the subclass, and ApplicationUser everywhere the framework...

Custom Comparers for Sorting a List in C#

Custom Comparers for Sorting a List in C#

In C#, every way of sorting a List<T> comes down to where the comparison lives: in the type itself with IComparable<T>, in a separate comparer with IComparer<T> or Comparer<T>, or in a Comparison<T> delegate. List<T>.Sort() takes...

Value vs Reference Types in C# – In Depth Guide

Value vs Reference Types in C# – In Depth Guide

In this article, we will learn about the categories of C# data types. We will deep dive into the differences between value types and reference types, what are they and what's the behavior of each type when instantiated, compared, or assigned. It's important to...

What Is Model Builder and How to Use It in ML.NET

What Is Model Builder and How to Use It in ML.NET

In this article, we will take a look at ML.NET Model Builder for Visual Studio. Following our introductory article on ML.NET, in this one, we will learn what it is and how to use it in our projects. [sc name="github"...

Source Generators in C#

Source Generators in C#

Traditionally, code generation in C# involved using external tools or templates to generate code before the compilation process, like the T4 template. However, with source generators, code generation becomes an integral part of the compilation itself. In this article,...

How to Merge Dictionaries in C#?

How to Merge Dictionaries in C#?

Dictionary<TKey, TValue> has no built-in method that merges dictionaries, so we merge them ourselves: with a foreach loop that copies every key-value pair into a new dictionary, or with LINQ methods such as Concat(), GroupBy(), ToLookup() and Union(). These...

NSubstitute: Effective Mocking in .NET

NSubstitute: Effective Mocking in .NET

NSubstitute is a mocking library for .NET that reads like plain C#: Substitute.For<IEmailService>() creates the mock, service.IsValid(x).Returns(true) configures it, service.Received().Send(x) verifies it. No Mock<T> wrapper, no .Object property, no lambda...

What is Locking and How to Use a Locking Mechanism in C#

What is Locking and How to Use a Locking Mechanism in C#

 In this article, we will discuss the locking mechanism in C#. We will explain what locking is and why we need it. We will demonstrate and explain different ways how we can use the mechanism.  [sc name="github"...

Sending Email With FluentEmail in .NET

Sending Email With FluentEmail in .NET

In this article, we are going to learn how to send emails using FluentEmail in an ASP.NET Core Web API application. FluentEmail is a popular open-source library for sending emails from .NET applications. It provides an easy-to-use fluent interface. That means we can...

XML Deserialization in C#: How to Deserialize XML

XML Deserialization in C#: How to Deserialize XML

XML deserialization turns an XML document into a C# object. We describe the shape we expect as a class, hand the document to XmlSerializer, and get back an object with its properties filled in. Without a class to map onto, the alternative is reading and parsing an XML...

Saga Pattern With NServiceBus in C#

Saga Pattern With NServiceBus in C#

Navigating the world of distributed systems and microservices in C#? Then we should appreciate the combination of the Saga Pattern and NServiceBus. This powerful pairing provides us with an efficient solution for coordinating long-running, distributed transactions in...

Should We Use Records or Classes or Structs in C#

Should We Use Records or Classes or Structs in C#

Classes and structs have been a part of C# since version 1.0, but more recently records have been added. In this article, we will compare these three structures to see how they each have a use-case where they are the best data structure to use. [sc name="github"...

Unable to Resolve Service for Type: How to Fix It in .NET

Unable to Resolve Service for Type: How to Fix It in .NET

InvalidOperationException: Unable to resolve service for type 'X' while attempting to activate 'Y' means the dependency injection container was asked to build Y, found a constructor parameter of type X, and had no registration for X. The full message looks like this,...

Scrutor in .NET: Assembly Scanning and Decoration

Scrutor in .NET: Assembly Scanning and Decoration

Scrutor adds two things missing from .NET's built-in dependency injection container: assembly scanning, which registers all our services in one line instead of dozens, and decoration, which wraps a registered service with logging, caching, or validation without...

How to Efficiently Randomize an Array in C#

How to Efficiently Randomize an Array in C#

In this article, we will look at how we can efficiently randomize an array in C#. We'll explore several ways of achieving that and then we'll compare the performance of each approach. [sc name="github"...

MediatR Publish and Send Methods

MediatR Publish and Send Methods

In this article, we are going to explore the key differences between the Publish and Send methods in MediatR, a powerful .NET library for component communication. [sc name="github"...