Tracing .NET Applications Easily With OpenTelemetry
One of the fundamental concepts in the OpenTelemetry project is the ability to observe end-to-end requests through our applications, which is known as tracing. We achieve this by adding custom code to our .NET applications to emit this tracing information, which we...
Bogus in C#: Generate Realistic Fake Data in .NET
Bogus is a .NET library that generates realistic fake data, such as names, emails, addresses and whole object graphs, from rules we write once per class. We'll use it to fill C# objects, make the output repeatable for tests, and save it to an EF Core database. Then...
Required Members in C#
C# 11 introduces the concept of required members. Any code instantiating the containing class must initialize these members; otherwise, the compiler will issue an error while compiling it. In this article, let's learn how to use this feature to prevent bugs and catch...
How to Randomize a List in C#
C# has several built-in methods for working with arrays and lists, but what if you need to randomize a list of items? In this article, we'll look at some techniques we can use to randomize lists in C#. We'll also discuss some of the pros and cons of each technique and...
How to Get The List of Properties in C#
In this article, we are going to learn how to get the list of properties in C#, and also how to use the BindingFlags to filter the properties to retrieve. [sc name="github"...
Non-nullable Property Must Contain a Non-null Value When Exiting Constructor (CS8618)
Compiler warning CS8618 tells us that a non-nullable member can still be null when the constructor finishes. The compiler prints it in a form that depends on what we declared. For a property and a field, the two forms are: Non-nullable property 'FirstName' must...
Convert String and Int to Enum in C#
Converting a string to an enum in C# is a call to Enum.Parse<T>() or Enum.TryParse<T>(). Converting an int to an enum is a cast. That asymmetry is the whole subject: one direction goes through a method that can reject bad input, and the other goes through...
Understanding PLINQ in .NET
In this article, we are going to explore PLINQ, which is the parallel implementation of LINQ. We are going to focus on when, and how to efficiently use it. [sc name="github" url="https://github.com/CodeMazeBlog/CodeMazeGuides/tree/main/csharp-linq/UnderstandingPLINQ"...
Boxing and Unboxing in C#
In this article, we are going to talk about boxing and unboxing in C#. When we work with C# language constantly, we need to...
Encrypting and Decrypting a String in C#
In this article, we will learn how encrypting and decrypting a string is done using C# and the Cryptography package that comes included with .NET. [sc name="github"...
Cryptography Implementations in .NET
Cryptography is the security backbone of what our modern society is built upon. This science has enabled us to keep data and communication safe from being compromised or stolen. We can find examples of its use in personal password managers up to the security aspect of...
Generic Attributes in C#
In this article, we are going to learn how generic attributes help us implement better custom attributes through a practical example. [sc name="github" url="https://github.com/CodeMazeBlog/CodeMazeGuides/tree/main/csharp-advanced-topics/GenericAttributesInCSharp"...
Rebus in C#: The Lightweight .NET Service Bus
Rebus is a lightweight, MIT-licensed service bus for .NET. We publish a typed message, implement IHandleMessages<T> to receive it, and Rebus takes care of the exchanges, queues, retries, and the error queue. It sits between our code and a broker such as...
How to Iterate Over JSON Objects in C#
Manipulating JSON objects is a common operation in applications. In this article, we will look at different ways of iterating over JSON objects in C#. [sc name="github" url="https://github.com/CodeMazeBlog/CodeMazeGuides/tree/main/json-csharp/IteratingOverJsonObject"...
OpenTelemetry in .NET – Basic Usage With Examples
Being able to debug our applications in an efficient manner is a very important aspect when it comes to designing and building applications. In a world of microservices, it becomes harder and harder to reason about our applications and where the source of a bug or...
ConcurrentBag in C#
ConcurrentBag<T> is a thread-safe, unordered collection in the System.Collections.Concurrent namespace. Any number of threads can add to it and take from it at the same time without a lock statement. The price of that is order. A bag keeps one queue per thread...
How to Add a BearerToken to an HttpClient Request
Often, in our daily routine, we have to deal with secure APIs and use a BearerToken to make HTTP requests. In this article, we are going to learn the correct way to add a BearerToken to an HttpClient request. [sc name="patreon-source-code"...
Task vs ValueTask in C#: When to Use Each
Return Task by default. Use ValueTask only for a method that is called constantly and usually finishes without ever awaiting anything (a cache lookup that normally hits, a buffered read that normally has data). The reason is what each one costs when it completes...
ASP.NET Core Select Tag Helper: asp-for and asp-items
The select tag helper renders a <select> element from a model property. Two attributes do the work: asp-for names the property, and asp-items supplies the options. asp-for carries more weight than it looks. It sets the element's id and name, it decides which...
Update a Dictionary Value in C#
To update a value in a dictionary, assign to it through the indexer: toppings["pepperoni"] = 8;. If the key is already there, the old value is replaced, and if it is not, the key and the value are added. A set never fails because a key is missing. The trouble starts...
Default Interface Method in C# And When to Use It
In this article, we will learn about default interface methods in C#. So, let's start. Introduction to a Default Interface Method The...


