Does readonly make your code faster?
In this blog post we will discover whether or not the readonly
modifier can make your code faster. So without further ado let's get started.
In this blog post we will discover whether or not the readonly
modifier can make your code faster. So without further ado let's get started.
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.
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.
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.
.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.
C# 11 brings some major improvements for structs. You remember Span<T>
, which you couldn't create?
Now you can. C# 11 allows ref
fields in a struct. Why this is a cool thing and what you can do with it, you will see in a minute.
Finally the long awaited bulk update and bulk delete feature is heading towards Entity Framework 7. So we will have a look how to use it and what might be some limitations.
Also I will have a quick look at the performance aspect, because this was the main driver for that feature.
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.
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.
In this blog post I will show you 4 different ways of creating an array and how they differ from each other.
.NET ships a nice StringBuilder
since the dawn of time or at least since the beginning of the framework itself. The intention is simple: If we need to concatenate a lot of strings we can't rely on the + operator
as we introduce a lot of unnecessary allocations plus it is slow!
That is where the StringBuilder
jumps into the picture.
Why is that? Why is the StringBuilder
better? And can we do better? Spoiler: Yes!
SSE stands for Streaming SIMD Extensions. What does that exactly mean and how can we leverage that in our C# code?
This blog post will shed some light on this topic. Also we make a small comparison to PLINQ.
Blazor is a beautiful framework for building SPA's. One advantage is that those SPA's feel like native applications. We do not feel that stuff is necessarily loading. But for that we have to keep our code performant.
This guide will highlight some of the techniques to keep your Blazor app running smoothly. We will also cover some more advanced use cases.
In this article I will shade some lights on some of the most used terms which seems very confusing especially for beginners: heap, stack and boxing and unboxing.
Furthermore we will also encounter internet wisdom like:
Value types get stored on the stack. Reference types on the heap
We discuss why this is wrong and what the hell performance has to do with it?
Did you know that you can spend 100x times longer depending how you compare your enums?
Of course we are speaking here in terms of nanoseconds but this story is much more then just numbers. We'll also talk about boxing and unboxing. So let's dive right into it.
Let's have a small look into struct
s and how they work when using Equals
and GetHashCode
. Plus have a brief look into a new C# 10 feature: readonly record struct
.
A story about boxing / unboxing and string interpolation in C#. What has string interpolation to do with boxing / unboxing and what's the impact?