Latest Posts On Code Maze

Alternative Way of Logging With OpenTelemetry Logging in .NET

Alternative Way of Logging With OpenTelemetry Logging in .NET

One of the most well-known debugging tools for developers is undoubtedly application logging, which allows us to record timestamped information about the current state of our application. In the OpenTelemetry project, logs are the third and final pillar of...

How to Find the Caller Method in C#

How to Find the Caller Method in C#

In this article, we will learn how to find the caller method in C#. We will also learn about scenarios in which this information could be useful and how to retrieve it. [sc name="github"...

C# List to JSON: How to Serialize a List

C# List to JSON: How to Serialize a List

To serialize a list to JSON in C#, pass it to JsonSerializer.Serialize(). One List<T> in, one JSON array out, and nothing to install, because System.Text.Json ships with .NET. There are three more routes worth knowing. SerializeToUtf8Bytes() returns UTF-8 bytes...

C# yield break: How It Differs From break and return

C# yield break: How It Differs From break and return

The yield break statement ends an iterator method on the spot. The sequence stops, and nothing after it in the method runs except a finally block. Think of it as the iterator's version of a plain return;, which the compiler doesn't allow inside an iterator. In the...

Testing Exceptions in MSTest, NUnit, and xUnit

Testing Exceptions in MSTest, NUnit, and xUnit

In this article, we will take a look at testing if our code throws exceptions, why we want to do that, and how to do it in the MSTest, NUnit, and xUnit frameworks. [sc name="github"...

TimeSpan in C#: Create, Format and Parse

TimeSpan in C#: Create, Format and Parse

TimeSpan is a value type that represents a length of time rather than a point in time. A DateTime says when; a TimeSpan says how long. It stores one 64-bit tick count, where a tick is 100 nanoseconds. That single value is what makes the type cheap, comparable and...

How to Read Connection Strings in .NET

How to Read Connection Strings in .NET

Β In this article, we will learn how to read database connection strings from different configuration sources in .NET applications. [sc name="github" url="https://github.com/CodeMazeBlog/CodeMazeGuides/tree/main/aspnetcore-features/HowToReadConnectionStringsInDotNet"...

Read and Parse a JSON File in C#: 6 Ways

Read and Parse a JSON File in C#: 6 Ways

To read a JSON file in C#, open it with File.OpenRead() and hand the stream to JsonSerializer.Deserialize<T>(). Two calls, no NuGet package, and the file never becomes a string we throw away, which is why it is the fastest of the six methods we benchmark below....

IEnumerable vs ICollection vs IList vs List in C#

IEnumerable vs ICollection vs IList vs List in C#

These four are one inheritance chain, not four alternatives. IEnumerable<T> can only be iterated, ICollection<T> adds Count and mutation, IList<T> adds indexing, and List<T> is the concrete class that implements all of them. So the choice is...

Convert a File to a Byte Array in C#

Convert a File to a Byte Array in C#

In this article, we will learn about situations where we may need to convert a file into a byte array. Additionally, we will learn two ways to perform the conversion in C#. If you want to learn how to convert a byte array to a file, check out our convert byte array to...

Different Types of Comments in C# and Should We Use Them

Different Types of Comments in C# and Should We Use Them

In this article, we are going to talk about different types of comments in C#. The compiler will compile any syntactically correct code into an intermediate language, and after that, the .NET runtime will execute this IL code on the client computer. The computer will...

Resolving the Call Is Ambiguous Error While Using LINQ

Resolving the Call Is Ambiguous Error While Using LINQ

In this article, we will learn when the "call is ambiguous" error happens, why the error happens, and how to work around the issue. There are still so many legacy projects using older versions of EF Core, and in those projects, this error can cause a lot of headaches...

Create Clean Guard Clauses With GuardClauses in C#

Create Clean Guard Clauses With GuardClauses in C#

In this article, we discuss how to create guard clauses, how they differ from user input validation, and how to improve our guards by writing clean guard clauses leveraging the GuardClauses library. [sc name="github"...

C# FileInfo vs File Class: When to Use Each

C# FileInfo vs File Class: When to Use Each

File and FileInfo both create, open, move and delete files in C#. File is static and takes the path on every call, while FileInfo wraps one file in an object and caches what it reads about it. We use File by default and switch to FileInfo when one file goes through...

Determine Whether Two Date Ranges Overlap in C#

Determine Whether Two Date Ranges Overlap in C#

In this article, we will discuss how to determine whether two date ranges overlap in C#. We will explore how to achieve this result using the DateOnly and TimeOnly records and then, we're going to beautify our implementation with a custom class representing date...