Latest Posts On Code Maze

Fast Inserts With Entity Framework (EF Core)

Fast Inserts With Entity Framework (EF Core)

In this article, we will review multiple ways of performing fast inserts of bulk data into a database using the Entity Framework (EF for short). Entity Framework "classic" was introduced in 2008, was superseded nearly a decade later by EF Core, and continues to add...

Easy Sorting, Filtering and Pagination in .NET With Sieve Package

Easy Sorting, Filtering and Pagination in .NET With Sieve Package

Most applications that are developed nowadays usually involve interacting with non-trivial amounts of data, so we need to provide functionality for sorting and filtering in our applications. Moreover, speed and load times are increasingly important, so we don't want...

How to Return XML From ASP.NET Core Web API

How to Return XML From ASP.NET Core Web API

ASP.NET Core is a powerful and flexible platform for building web applications. It supports multiple data formats, including JSON and XML, and provides many tools for serialization and deserialization. In this article, we will focus on how to return XML from ASP.NET...

Short Circuit Evaluation of IF Statements with Await in C#

Short Circuit Evaluation of IF Statements with Await in C#

In this article, we are going to look at what exactly short circuit evaluation is in C# and how asynchronous calls are affecting it. [sc name="github" url="https://github.com/CodeMazeBlog/CodeMazeGuides/tree/main/async-csharp/ShortCircuitEvaluationWithAsyncAwait"...

How to Serialize Exceptions as JSON in .NET

How to Serialize Exceptions as JSON in .NET

In this article, we will learn how to serialize exceptions as JSON in .NET. Good exception handling is one of the key aspects of a successful enterprise application. And when we talk about an API, it's important that we provide relevant failure details to consumer...

Resource-Based Authorization in ASP.NET Core

Resource-Based Authorization in ASP.NET Core

When authorizing requests in ASP.NET Core Web API projects, it's not enough to know the endpoint and the user; we also need to be aware of the specific resource the user is trying to access. In this article, we will learn how to tackle these situations using...

C# JSON Property Order: System.Text.Json and Newtonsoft

C# JSON Property Order: System.Text.Json and Newtonsoft

To control property order in JSON output we set it explicitly: [JsonPropertyOrder] in System.Text.Json, and [JsonProperty(Order = …)] in Newtonsoft.Json. Converters and contract resolvers exist for the cases those two attributes cannot reach, such as sorting a whole...

Aggregate Design in .NET: Aggregate Roots and Invariants

Aggregate Design in .NET: Aggregate Roots and Invariants

An aggregate groups domain entities that change together, and the aggregate root is its main entity. The root is the only way into the aggregate, and it enforces the invariants, the business rules that must hold at all times. We will get to know the main design...

Split a String in C#: Overloads and StringSplitOptions

Split a String in C#: Overloads and StringSplitOptions

String.Split() cuts a string into an array of substrings at every occurrence of a separator we name. "a,b,c".Split(',') returns ["a", "b", "c"]. The separator can be one character, one string, or an array of either, and every form accepts an optional...

Upcasting and Downcasting in C#

Upcasting and Downcasting in C#

Upcasting and downcasting are important concepts in C# programming that allow us to convert an object of one type to another type. These concepts are essential to work with polymorphism and object-oriented programming. In this article, we will explore how upcasting...

How to Validate an Email Address in C# With or Without Regex

How to Validate an Email Address in C# With or Without Regex

To validate an email address in C#, we can parse it with MailAddress.TryCreate(), check it with the EmailAddressAttribute, match it against a regular expression, or use FluentValidation's EmailAddress() rule. Each one lets different inputs through, and none of them...

How to Round Down a Number to the Nearest Integer in C#

How to Round Down a Number to the Nearest Integer in C#

To round a number down to the nearest integer in C#, we call Math.Floor() and cast the result to int: (int)Math.Floor(-1.25) gives -2. A plain cast gives -1 instead. Negative numbers are where most of the usual candidates go wrong. So we test the methods that would...

LINQ Join in C#: How to Do an Inner Join

LINQ Join in C#: How to Do an Inner Join

An inner join in LINQ pairs each element of one sequence with every element of a second sequence that has the same key, and leaves out the elements that have no match. In C#, we write it with the join clause or the Join() method. We'll join two collections in both...

C# Empty Array: Declare, Create and Initialize

C# Empty Array: Declare, Create and Initialize

An empty array in C# is Array.Empty<T>(), or [], which the compiler turns into the same thing. Both hand back one shared zero-length array instead of allocating a new one. The older spellings still work. new string[0], new string[] { } and string[] myArray = {...