Span / Memory / ReadOnlySequence in C#
There are many different memory types used in modern C# programs. The more common ones are Span<T>
and Memory<T>
. Occasionally there is also ReadOnlySequence<T>
. What do these types do?
There are many different memory types used in modern C# programs. The more common ones are Span<T>
and Memory<T>
. Occasionally there is also ReadOnlySequence<T>
. What do these types do?
With the upcoming release of .NET, the team introduced an abstraction of time itself. That can bring you major benefits especially if you have to test scenarios where time is a crucial part! Until now, you had to create your own wrapper. This, of course, makes integration with 3rd party libraries tricky.
With preview 4 of .NET 8, a new terminal logger was introduced. It basically removes a lot of the noise that the default logger produces. It also adds some nice colors to the output. Let's see how to use it!
The nameof
operator is a great way to get the name of a variable, type, or member. With C# 12 it's getting even better. Let's see how.
TryGetNonEnumeratedCount
attempts to determine the number of elements in a sequence without forcing an enumeration. It returns true
if it could and false
if it couldn't. The API was added with .NET 6 - let's have a look at how that thing works.
I did already write about some useful extension methods for Task
and ValueTask
. Today I want to show you some useful extension methods for IEnumerable
.
In this blog post, we will explore some cool things you can do with ValueTuple
. Also, a short explanation of what ValueTuple
is and how it works.
In Entity Framework 7, the team has added support for ordered indexes to the fluent API. In this blog post we will look at how to use this feature and what it means for your database.
In this blog post, we will have a look at the different log levels and how to control them.
Microsoft's integrated dependency injection (short DI) container is very powerful, but there are also certain pitfalls. In this article, I will show you what some of the pitfalls are and how you can verify them.
In this small blog post, I will show you how to create your own Validation attribute in ASP.NET Core to tailor-made your validation rules.
In this blog post, we will discuss the "Unit of Work" pattern and how it can be used to implement domain events in a DDD application. For that, we will also discuss how we can leverage middleware to implement the "Unit of Work" pattern in a .NET application. A lot of things are going to happen in this blog post, so let's get started.
Let's talk about Contravariance and Covariance in C# using .NET Framework examples!
Contravariance and covariance are essential concepts in C# when dealing with generics, enabling us to have more flexibility when assigning generic types. So let's have examples straight from the framework itself!
Also, we will go a bit deeper and talk what some differences between generic constraints and things like Contravariance are.
As with every .NET release, Microsoft improves the performance of the runtime and guess what: This release is no exception to this. In this blog post, I want to go through some of the improvements made so far (.NET 8 preview 3).
In this short blog post, I will show you 5 useful extensions for Task
Source Generators are more and more an integral part of the .NET ecosystem. But how does that play together with everyone's favorite: Regular Expressions?
In this blog post we will dive in how we can leverage source generators in combination with regular expression to have a debuggable, but also very performant way of executing regular expressions!
In this blog post, we will explore the use of SIMD instructions to speed up LINQ queries. We will use the Vector
In this blog post we will create a ToolTip component in Blazor from scratch. We will use the Blazor WebAssembly template to create a new project. We will then add a ToolTip component to the project and use it in the Index page. We will also add some styling to the ToolTip component.
The advantage over using a library is that we can customize the component to our needs as well as keeping it simple! So let's get started!
Source generators are a powerful feature introduced to C#, allowing developers to generate additional code during the compilation process automatically. They can help reduce boilerplate, improve performance, and simplify your codebase.
This blog post will introduce source generators, discuss how they work, and walk through an example of a source generator for generating build information.
Did you know you can use the 'is not' operator with exception filtering to simplify and improve your error handling in C#?
In this short blog post, I will show you how to use it.
In this blog post, we will discuss how we can "cache" entries from the database. We will talk about why we would do this in the first place and how to achieve that.
Also, we will talk about some implications and what "cache invalidation" is.
Sometimes you have to map an object to another representation in C#. And you think: Why isn't C# duck-typing capable?
You might hear of libraries like AutoMapper that do the tedious work of mapping one object to another with the same structure. This blog post will give a super simple introduction to how those libraries are working internally.
In the end a bit of a subjective topic on whether or not I would use such libraries.
Skip
and Take
are used for pagination or limit the number of elements returned by a query.
Since C# 8 you can use Take
with a Range
. Let's see some examples.
In this blog post we will discover how to write your own small task scheduler / job scheduler with cron notation in ASP.NET Core. You might know similar approaches under the name of Quartz or Hangfire.
With the help of BackgroundService
we will build our own, lightweight version of it.
In recent months and years, there was a certain hype around benchmarking, also in the .NET community.
This blog post is meant to ground some of the benchmarking topics and put this into relation to other things so you get a better understanding if it's worth the effort.
I will put that simple question in the room: "Is a square a rectangle?" And you might thank: "Well dah, of course!"
But wait for a second and let's check it together. We will use the L in SOLID: The Liskov Substitution principle to check if this relationship makes sense for us!
The Humble Object Pattern is a design pattern to make especially unit testing easier with the goal of separating behaviors that are easy to handle (domain logic) from behaviors that are hard to handle (like external events or dependencies).
So let's have a look at what it is and how you can utilize it.
A bit back on LinkedIn, there was a discussion about read-only collection and immutability where this is not the point I want to discuss now, as I already covered that here: "ReadOnlyCollection is not an immutable collection".
This post is just about the performance of those types compared to our baseline, the good old List<T>
. It also explains why we see the results we see.
In this article, we will discuss the testing pyramid - what it is and what are some problems with that.
We will also discuss a different approach: The testing diamond.
Did you ever need git-specific information like the latest tag or the current commit inside your C# code? Or even the semantic version number of your current build=
Well, there is an easy solution involving source generators.
Did you see the following error in recent days in your build-pipeline:
error NETSDK1194: The "--output" option isn't supported when building a solution.
If so - that is not necessarily your fault at all! Microsoft released a new SDK version, which breaks your builds. Let's see why and what we can do to tackle that.
System.Text.Json.JsonSerializer
has a weird quirk in regard to performance and memory management. So we will discuss what is "wrong" with this code: JsonSerializer.Serialize(myObject, new JsonSerializerOptions(...));
.
Did you ever ask yourself: What is a middleware, and why should I use it?
If so, this blog post is exactly for you. We will see where we could use a middleware and also how we can use the Dependecy Injection container of ASP.NET Core.
Multi-tenancy is a software architecture pattern where a single instance of a software application is used by multiple customers, with each customer having separate and isolated data, configurations, and resources. RavenDB is a NoSQL document database that provides a flexible and scalable solution for multi-tenant applications. This blog post will explore why multi-tenancy exists, the advantages of using RavenDB for multi-tenant applications, and provide code examples to get you started.
In this blog post, we'll dive into the ins and outs of the repository pattern and examine both its benefits and its potential drawbacks. We will start from the very basic to some more advanced use cases. So let's dive right into it.
Did you ever hear the word "compiler magic" or "syntactic sugar"? Probably yes and therefore we want to dissect what this "magic" really is!
We can see how we can predict performance or bugs by "lowering" our code. Also we will see how things like foreach
, var
, lock
, using
, async
, await
, yield
, anonymous types, record
, stackalloc
, pattern matching, Blazor components, deconstructor, extension methods... do not really exist.
... if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.
This is written in the Linux style guide. Let's see why they have that rule and how we can overcome deeply nested code.
Another new C# 12 feature might drop soon and makes its debut with the next iteration: Primary Constructors.
The blog post will talk about what a Primary constructor is, why we already have it, and what the proposal tries to change. Exciting times are ahead of us!
Cohesion represents the degree to which the elements of a module belong together. A module or class is said to be highly cohesive if its methods and data are highly related, meaning that a change in one affects just a small number of elements.
We can use this metric to know whether or not an object is in a good shape or needs some refactoring.
Pagination is the process of dividing a set into discrete pages. In the context of Entity Framework, that means we are only getting a certain amount of entries from the database.
And we will implement a very easy solution to make that happen in 3 steps. The result will look like this:
var pagedList = DbContext.BlogPosts.ToPagedList(page: 1, pageSize: 5);
I often read that Task
is used for multithreading in C# / .NET, but that is not the case. And it is crucial to understand why this isn't the case. We will also see which problem exactly Task
is solving in the first place.
I want to showcase two of my many side projects. My .NET Tips and Tricks website, where I collect and categorize, well, tips and tricks around .NET-related topics as well as my ValueStringBuilder
.
Strings are one of the most universal data types. We use them for URLs or regular expressions or even to define some date. With .NET 7 we have a new way of giving those strings a bit of meaning. Meet StringSyntaxAttribute
.
I also show you a way how to use them in .NET 6 and earlier.
There is still a long road ahead of us until the release of .NET 8, but the first new language constructs are getting public. The first one I want to present is: Default Parameters in Lambdas.
Many know that you can take ReadOnlySpan<char>
objects when dealing with string
s. They give you a direct way of operating on the underlying memory. Often times you can use them interchangeably, but there are scenarios where you really have to watch out what is going on.
This blog post will have a look at a major problem with ReadOnlySpan
when used like a "regular" string
.
Does your Dependency Injection container is one big pile of method calls one after the other?
Are there 50 lines of just AddScoped
, AddTransient
, and so on? Well, let's fix this.
We can utilize extension methods to make an order to that mess!
.NET knows a big list of collection-like types like: IEnumerable
, IQueryable
, IList
, ICollection
, Array
, ISet
, ImmutableArray
, ReadOnlyCollection
, ReadOnlyList
, and many more.
This blog post will give you an exhaustive list of types in .NET and when to use what.
This blog post will show you a very simple Dependency Injection container.
This will give a better understanding of what Dependency Injection is and how it is done. And sure we will see how this is related to IoC - Inversion of Control.
Sometimes you have an Id
of an object and want to delete the underlying thing from the database. But it doesn't make sense to load the whole object from the database to memory first. So how can we achieve this quickly?
ChatGPT is going viral right now. Besides funny conversations, you can use that tool to generate also code for you. But does it really hold up to its reputation and it is a serious pairing partner? Let's see.
In the past, it was tricky to get if the current process runs under elevated rights. In the sense of it is run under the sudo group in *nix (Unix, Linux, macOS, you name it) or administrative rights in Windows.
But that changes with .NET 8 as we now have a unified API for that.
Abstraction and Encapsulation are two fundamental concepts in object-oriented programming. So let us have a small look what the difference is between those two.
Did you ever wonder how we can iterate through a StringBuilder
? I mean, of course, we can just call ToString
and use the returned string, but that means we materialize the whole thing without good reason.
We can also use a normal for-loop. But we can also find a completely different and probably dumber way! And if you wonder: No, this is not something you do in your daily life, but by doing so, I can show some cool stuff C# and .NET offer.
Do you remember how .NET 6 introduced the ArgumentNullException.ThrowIfNull
guard? And afterward, with .NET 7 we've got this excellent bit: ArgumentException.ThrowIfNullOrEmpty
? Guess what, there might come some new handy additions for the upcoming .NET 8 iteration.
So let's see what are those new changes and how they make the code simpler.
Analyzers did become an integral part of the .NET ecosystem. Their main responsibility is to find potential code issues and warn you. Often times this comes even with potential fixes you can directly apply.
And Microsoft will continue this journey with the upcoming .NET 8 release. This blog post will show you potential candidates, which will make the cut.
.NET 7 was freshly released but Microsoft does not sleep. .NET 8 is already in the making and I want to showcase to you one new area where the dotnet team is working on Frozen collections.
So let's have a look at what frozen collections are and how they are working.
Use always a
StringBuilder
That is what you can read from time to time. The basic idea is, that a StringBuilder
is "better" to be defined.
Why are people telling that lie? Let's discuss this and see what it isn't true.
Often times we hear about allocations on the heap. How can we easily measure this? This article will show you a very easy way of doing so.
Plus we will answer the question how big is an empty array? And if you think 0 bytes, then spoiler alert, that is not the case at all.
Often times we have unit or integration tests that rely on some input data. The easiest solution is just to take some hard-coded values and move on with life. This has some major downsides:
Giving specific values in a test carries meaning, but we are often times not interested in that. We just need to pass the object around to fulfill the API. Also the simplest solution to fulfill your test is literally checking against those values.
Here is an elegant solution to that problem: AutoFixture
. I will show you what it can do, especially in combination with xUnit
.
You might have heard that mutable value types are evil. But why is that and why does the .NET framework use them then? Are they really that evil?
Let's have a look at a few examples and have a look what is going on!
Often times your API in your program or library evolves. So you will need a mechanism of telling that a specific API (an interface or just a simple method call) is obsolete and might be not there anymore in the next major version.
Also it can happen that you have a preview version of a API, which might not be rock-stable and the API-surface might change. How do we indicate that to the user?
This edition has the following infographics:
DebuggerDisplayAttribute
ExceptionDispatchInfo
implicit
and explicit
operator.NET knows local functions and lambda expressions. You can almost take them interchangeably, but there are also some differences between them.
This article will show the differences between them.
You might have read, that re-throwing an exception like this: throw exc;
is considered bad practice and you should just do this: throw;
instead.
But why is it like that?
In this article we will have a closer look at LiteDB, a .NET NoSQL Document Store in a single data file. We will discover the advantages of LiteDB and why it is a viable candidate for your next project.
We will also explore what are the differences between a NoSQL and a classical SQL database are and what this has to do with the reminiscent SQL CE or the more modern SQLite database.
In this short blog post I want to show you two silly things so that you can apply right now! Both of them equally silly, but that is not the point (is it ever?).
We will see how to await an integer or TimeSpan
and how to foreach through an integer. All of this thanks to the magic of extensions methods.
This edition has the following infographics:
ConfigureAwait
on IAsyncDisposable
foreach
struct
null
for a Task
This article will show you what exactly a WebApplicationFactory
is and why it is so "mighty" when it comes down to testing. I will highlight some of the properties, which you can leverage to write powerful tests.
Sometimes I publish parts of my infographics I publish on various channels with more explanation.
And then sometimes I don't. This time I just put some of my (hopefully self-explanatory) infographics here.
In this article I will show you what is the difference between x86 (32-bit) vs x64 (64-bit) in the .NET World.
What is the impact if you choose on or another.
Pattern matching, which was introduced in C# 9, is a hell of a beast and does more than you might think.
In this small blog post I show you where the magic is and what it does under the hood.
Did you ever wonder what is a nice way of structuring your Blazor application?
I will show you how I structure my Blazor projects (as well as this very blog). What are the upside in contrast to the "default" structuring you get with the Blazor template.
This is a small story about how memory operates in your .NET application. Well not only .NET but how memory does or does not get allocated.
We will see how a 1 Gigabyte big array is only a few megabytes big to some extend. Furthermore I will discuss working set and committed memory.
NDepend is a static analysis tool for .NET managed code. The tool proposes a large number features, from dependency visualization to Quality Gates and Smart Technical Debt Estimation. For that reasons the community refers to it as the "Swiss Army Knife" for .NET Developers.
So let's check if that descriptionI shamelessly stole from Wikipedia checks out and what we can do with that tool.
Often times it is easier to have a nice illustration at hand, which explains you things the easy way. So let's do this for a lot of LINQ operations like Where
, Select
and friends.
Of course a small explanation will be attached as well.
You might have code where an object offers you an event to notify you when a specific operation is done. But event's can be tricky to use, especially when you want to have a continuous flow in your application.
That is where TaskCompletionSource
comes into play. We can "transform" an event based function into something which is await-able from the outside world via the await
keyword.
When you get started or even if you have quite some knowledge it can be confusing to juggle with those terms. So what is the difference between C# and .NET? And what does it have to do with IL and JIT?
C# offers a lot of utility especially around the delegate topic. So let's see what exactly a delegate is and how the distinct types like delegate
, Action
, Func
, Predicate
, anonymous function, lambda expressions and MulticastDelegate
behave. A lot to digest and discover so let's go.
I already made a post about how the Garbage Collector works in .NET and also introduced the topic of Generation slightly. This article will look a bit more in detail into why we have those mechanisms in the first place, including the Large Object Heap.
Just imagine a car pool: There is a dealer which bought the car and lent's it to you. After a while you will return this car where you got it from. Much like that works an ObjectPool in C#. You can rent an expensive object from the pool and when you are done with it, you just return it. Sounds beautiful, doesn't it?
Let's explore the advantages and disadvantages of a ObjectPools and how they work.
RavenDB is a well known open-source document-oriented databse for .NET. And of course we want to test our logic and not only locally while developing, but also our continuous integration pipeline should be able to run our tests. So let's tackle exactly that.
This article will talk about the garbage collector in .NET. Why do we have and need him? And why it is essential to understand the behavior to know what impact on our application he has.
What is Tail-Recursion? We will discover this "special" form of recursion on the example of the Fibonacci series. Also we will check how much faster it is and why.
In C# we can add the sealed
modifier to a class to indicate that no one is allowed to derive / inherit from that class.
Let's have a look at the compiler in certain scenarios what happens if you seal a class.
When we are passing objects around we can do this either via reference or by value. Which of those two methods is faster?
To answer this question we have to dive into a bit of info about what happens exactly when you pass something around and how the other side will receive this.
Besides the big announcements of .NET 6 there are also some smaller features. I want to show case a special one: static abstract interfaces. With this you have ability to extend the contract in that sense, that an implementing class has to provide also static methods. This feature is right now flagged as preview, but you can use it if you want.
This also enables generic math operations on an interface level.
.NET brings two types which seem very similiar
IEnumerable
IQueryable
What is the difference? Most are familiar with using IQueryable
when we want to go to the database and back. But why not using IEnumerable
?