How to Convert a C# Object to a JSON String in .NET
To turn a C# object into a JSON string, we call JsonSerializer.Serialize(obj) from System.Text.Json, which ships with .NET, or JsonConvert.SerializeObject(obj) from the Newtonsoft Json.NET package. Both return the JSON as a plain string. We'll use the two main JSON...
Constructors in C#
In this article, we will discuss Constructors in C# and different types of constructors. More importantly, we will discuss these constructors' differences and use cases. [sc name="github"...
Unsafe Code in C# (unsafe keyword)
In this article, we are going to learn about the unsafe code in C#. In general, the codeΒ that we write in C# is safe code. It creates managed objects and doesn't access the memory directly. On the other hand, unsafe code in C# is code that is not in direct control of...
Using Moq to Determine If a Method is Called
In this article, we are going to see how to determine if a method is called during test execution. To do that, we are going to use Moq and its verification APIs. [sc name="github"...
HttpClient vs RestSharp in C#: Which One to Use
HttpClient is the HTTP client built into .NET. RestSharp is a NuGet package that wraps it. That relationship settles most of the question of speed. RestSharp calls HttpClient underneath, so the two cannot differ by much: in our runs, the average call spent 44 ms or...
Testing Repository Pattern Using Entity Framework
Unit Testing is extremely important for creating robust software. It's very simple in principle but it can get a little bit tricky when it comes to real-world applications that depend on databases. In this article, we're going to explore some approaches to testing...
Shell Sort in C#: Implementation and Time Complexity
Shell sort runs insertion sort on elements a fixed gap apart, then shrinks the gap until it reaches 1. In C#, it fits in one method with three nested loops and no extra array. Have you ever needed to sort a list of items, but didn't want to use the built-in sorting...
How to Resolve Instances With ASP.NET Core DI
In ASP.NET Core dependency injection, we usually register injectable dependencies at the start of our program. We can then resolve these dependencies (services) much later once when we need them. Sometimes, however, we may need to resolve those dependencies even...
Ranges and Indices in C#
In this article, we are going to learn more about ranges and indices in C#, and how to use them to access a single or a range of elements in a sequence. We'll also see how ranges and indices help us write cleaner and more readable code. [sc name="github"...
Running .NET Core Applications as a Windows Service
In this article, we are going to learn how to create a .NET Core Worker Service and run it as a Windows Service. So let's get...
Lambda Expressions in C#
In this article, we're going to learn what Lambda Expressions in C# are in detail. Let's begin. What Are Lambda Expressions? Lambda...
Refit in C#: How to Consume REST APIs
Refit's own README describes the library as "The automatic type-safe REST library for modern .NET", and what that means in practice is that it turns an API into a C# interface. We declare the endpoints as methods with attributes ([Get("/users/{id}")]), and Refit...
How to Use Span in C# to Improve Application Performance
Performance is always an important factor in software development. It is not something only the developers of a framework must consider. When the .NET team released the Span<> struct they empowered developers to improve application performance, if used...
Ternary Operator ? : in C#
In this article, we are going to learn about the ternary operator (?:) in C#. Let's start. What is a Ternary Operator? Ternary Operator is...
Remove Duplicates From a List in C#
list.Distinct().ToList() removes duplicates from a list in C#. It is a LINQ method, its values come out in the order they first appear, and it is the right answer unless one of three things is true. If the list holds objects of a plain class rather than numbers or...
ApiController Attribute in ASP.NET Core Web API
In this article, we will learn about the ApiController attribute and the features it brings to our Web API controllers. Let's get...
Counting Sort in C#
Counting sort is a sorting algorithm for integers that never compares elements to decide their order. It counts how many times each value appears and writes the values back in order, so its running time depends on the number of elements and on the size of the largest...
How to Implement Retry Logic in C#
Often, we have transient problems in our application, such as a network failure, system rebooting, or anything else that throws an exception. In this article, we are going to learn how to implement retry logic in C# to help us handle these problems. [sc name="github"...
How to Call Generic Method Using Reflection in C#
In this article, we are going to learn how to call a generic method in C# using reflection. Generic methods are an all-time favorite of...
Method Overloading In C#
In this article, we are going to learn about method overloading in C#. In objected-oriented programming, method overloading is one of the important concepts to understand. It is a type of polymorphism that allows us to define methods that exist in different forms....
Pattern Matching in C#
In this article, we are going to talk about pattern matching in C#. We'll see different patterns in action with simple examples.Β Let's take a look at...








