Read a Text File in C#: The Fastest Ways
.NET gives us thirteen ways to read a text file, and two of them cover almost every case. File.ReadAllText() reads the whole file into one string. File.ReadLines() streams it one line at a time, so memory stays flat however big the file is. Everything else is a...
Using ML.NET CLI To Automate Model Training
As we continue our article series about ML.NET, we will look at the ML.NET Command Line Interface (CLI) tool.Ā Like ML.NET Model Builder, the ML.NET CLI uses AutoML to produce machine learning models.Ā [sc name="github"...
When to Use ReaderWriterLockSlim Over lock in C#
In this article, we will explore the lock statement and the ReaderWriterLockSlim class. We will discuss the situations in which it's better to use ReaderWriterLockSlim over lock in C#. We will also compare the ReaderWriterLockSlim class with the ReaderWriterLock class...
Serilog Log Levels and How to Configure Them
Serilog has six log levels, from least to most severe: Verbose, Debug, Information, Warning, Error, and Fatal. Serilogās own configuration documentation states the default: āif no MinimumLevel is specified, then Information level events and higher will be processedā....
DateTimeOffset vs DateTime in C#: Which to Use?
DateTimeOffset is the one to store. It carries the moment and its offset from UTC, so a value read back in another time zone still identifies the same instant. Microsoft Learn's guidance on choosing between the two says to "consider DateTimeOffset as the default date...
Synchronization Mechanisms – Volatile vs Interlocked vs lock in C#
When we apply multithreading to increase the speed and efficiency of our applications, synchronization plays an essential role for it to be effective. It maintains order when multiple threads try accessing the same resource. In this article, we'll explore different...
Different Ways to Overwrite a File in C#
In C#, we encounter various scenarios where we might need to overwrite an existing file. Whether to update the file or completely overwrite it, handling the overwrite efficiently is crucial to ensure data integrity and prevent data loss. In this article, we will...
Blazor InputSelect and @onchange: Handling Select Changes
On a plain <select>, @bind and @onchange cannot coexist. The compiler reports the onchange attribute as used twice. On an InputSelect, the same pair compiles without complaint, which is worse: the component captures our handler and then overwrites it with its...
How to Use Moq to Return a Value That Was Passed Into a Method?
In this article, we are going to learn about mocking with the Moq framework for .NET. Specifically, we're going to learn how we can mock a method, which returns a value passed to it. [sc name="github"...
Techniques for Checking Floating-Point Equality in C#
In this article, we will learn several techniques for checking floating-point numbers for equality. If we do not properly understand how floating-point values are stored internally, we might inadvertently write floating-point equality checks that do not behave as...
How to Use Stopwatch in C#
As software engineers, we know system precision and performance are essential, and the ability to measure time can come in handy to achieve this goal. That's where functionalities to capture and analyze time intervals can come in handy. We can leverage this to achieve...
Using HttpContext.Items to Pass Data With ASP.NET Core
In this article, we discuss how to leverage HttpContext.Items to preserve data in a request with ASP.NET Core. We can take advantage of this property to improve the efficiency of our applications. [sc name="github"...
File-Scoped Types in C# 11
The advent of C# 11 has brought with it a host of new features designed to improve and streamline the language's functionality. One of these enhancements is the introduction of file-scoped types, a unique concept designed to prevent name collisions among types,...
How to Use Basic Authentication With HttpClient?
In this article, we are going to discuss how to use basic authentication with HttpClient. While the topic may seem straightforward, there are a few different ways to solve this problem. We'll try and cover some of the main approaches, and hopefully, by the end, we...
Testing Using Testcontainers for .NET and Docker
Ā In this article, we'll explore how to use the Testcontainers library for testing .NET applications using Docker. Let's get...
Differences Between Any and Exists Methods in C#
In this article, we will explore the differences between Any() and Exists() methods in C#.Ā Although at first glance they might seem similar, they possess distinct characteristics and we use them in different scenarios in order to manipulate data collections. [sc...
Difference Between String, FormattableString, IFormattable in C#
C# provides different ways of manipulating strings, which can be helpful when designing and implementing systems. So, in this article, we will delve deep into the distinctions among String, FormattableString, and IFormattable in C#. Besides that, we will test whether...
Task.FromResult vs Task.CompletedTask vs Return in C# Async Methods
Task.CompletedTask, Task.FromResult() and a plain return all answer one question: how does a method hand back a Task when there is nothing to wait for? Return Task.CompletedTask when the method's return type is Task and the work is already done. Return...
Optimistic Concurrency in ASP.NET Core Web API
In this article, we'll discuss a few approaches to implementing optimistic concurrency in ASP.NET Core Web API. Let's begin. Understanding...
How to Return HTTP 500 From ASP.NET Core Web API
In this article, we will learn about the different ways to return a 500 status code with ASP.NET Core Web API controller actions. Let's start!...




