All posts with Tag C#

Preview image blogpost

Central nuget store for your UNO Platform App

With the newest UNO Platform update to .NET 6 we can leverage some cool things to simplify our dependency management. We combine .NET 6 with the new feature called Directory.Build.props to make that work.

Read More

Preview image blogpost

When does Blazor (not) render?

This blog post whill shed some light on when Blazor renders your content. Of course there are obvious candidates, but there are also some things to be aware about.

Read More

Preview image blogpost

Wrap Event based functions into awaitable Tasks - Meet TaskCompletionSource

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.

Read More

Preview image blogpost

goto in action - The exception from the rule

A lot of developers will say never use goto. This keyword is bad as it promotes spaghetti code. But as so often in live the exception confirms the rules. Let's see where goto can increase the readability and maintainability.

Read More

Preview image blogpost

What is the difference between C#, .NET, IL and JIT?

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?

Read More

Preview image blogpost

Delegate, Action, Func, Lamdba expression - What the heck!?

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.

Read More

The garbage collector in .NET - Part 2: Compacting

I already made a post about how the Garbage Collector works in .NET and also introduced slightly the topic of Generations. This article will look a bit more in detail why we have those mechanism in the first place including the Large Object Heap.

Read More

ObjectPool - Rent and return some instances

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.

Read More

Preview image blogpost

Blazor with an RSS Feed

If you use a public Blazor Server application (like this blog is) and you want to expose an RSS Feed (I know kind of old school) you can perfectly do that with Blazor Server and a little help from a nuget package from Microsoft. Furthermore this guide is not only valid for Blazor but every ASP.NET Core endpoint.

Read More

How to unit test a RavenDB

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.

Read More

The garbage collector in .NET

This article will talk about the garbage collector in .NET. Why do we have and need him? And why it is important to understand the behavior to know what impact on our application he has.

Read More

Preview image blogpost

Blazor with CancellationToken support

What happens when a user navigates away and still has a pending request to our server? Or what happens if we have a request which might take forever and wastes our resources?

Let's tackle these problems with a CancellationToken.

Read More

Preview image blogpost

Stop using Finalizers in C#

Finalizers (historically referred to as destructors) are in C# since its dawn of time and are used to cleanup resources. But they should not used in every use-case and can harm your applications performance.

Read More

Preview image blogpost

Tail-Recursion - Explained with the Fibonacci series

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.

Read More

Preview image blogpost

Why are sealed classes faster in C#? And should I seal them?

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.

Read More

Preview image blogpost

Passing by value or by reference - What is faster?

When we are passing objects around we can do this either via reference or by value. What of those two methods is faster?

To answer this question we have to dive a bit info what happens exactly when you pass something around and how the other side will receive this.

Read More

ReadOnlyCollection is not an immutable collection

In this blog post we discover how we can mutate a ReadOnlyCollection to have more or less entries than its original state. Readonly does not mean it is immutable. Also we will check out the ImmutableArray.

Read More

Preview image blogpost

Is public const bad?

Is declaring a number or string as public const considered bad practice? Let's have a look what a const variable means in the first place. Let's find out and also check what are the alternatives.

Read More

Preview image blogpost

Modal Dialog component with Bootstrap in Blazor

This short blog post will show you how to utilize Bootstrap to create a small and reuseable ModalDialogComponent.

Read More

Preview image blogpost

4 Different ways of creating an array

In this blog post I will show you 4 different ways of creating an array and how they differ from each other.

Read More

Preview image blogpost

default(MyStruct) vs new MyStruct() - what is the difference?

There are multiple ways to create a new struct, either via var myStruct = default(MyStruct) or via var myStruct = new MyStruct(); . Are there any differences?

Read More

From Zero to Production - Generate everything with a single button

This blog post will show you how to setup, from scratch, your GitHub repository so you can in a matter of a single click:

  • Run tests and build your application
  • Release the application for example to nuget
  • Create a Release on GitHub with Release notes
  • Update the documentation utilizing GitHub Pages and DocFx

Therefore we will build a "template" repository you can take as a foundation.

Read More

struct vs readonly struct vs ref struct vs record struct

C# knows struct since its down of time. But there are also recent additions like readonly struct, record struct and ref struct.

This article will show what are the differences between those 4.

Read More

Preview image blogpost

Create a low allocation and faster StringBuilder - Span in Action

.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!

Read More

Reusable loading Bar Component in Blazor

In our Blazor components we often call a repository or web API which takes some time. So it is nice to indicate to an user what the current state is. That is why we will create a small indicator even with steps to indicate how far we are in the progress.

Read More

Preview image blogpost

UNO Platform - Build a Todo App - Part 5

This is the last part of the series (for now 😄). In the last. Two parts are missing:

  • Drag and Drop the items from one lane to another
  • Save and load the state

And exactly those two things we will tackle. I also will give a small conclusion of my journey.

Read More

Preview image blogpost

UNO Platform - Build a Todo App - Part 4

In the forth part we will take care of our newly created todo item. Until now we clicked on a button and that's all. Sure we have a nice form where we can enter the basic information, but that is all.

The time has come to put our element into the respective swimlane. We will also learn how to add further nuget packages to our project.

Read More

Preview image blogpost

The Trie - A powerful data structure

A Trie is a common data structure used for example for auto completing words. We will discover how to built our own Trie and check how fast he competes against a "normal" Hashset.

Also we will build a TypeAHead input component which automatically offers us some suggestion based on our input. Of course, we will use our newly created Trie.

Read More

Preview image blogpost

UNO Platform - Build a Todo App - Part 3

In the third part of our small mini series: "Building an todo app with the Uno Platform" we will dive deeper into creating a modal dialog where we can enter the details of our todo item. We will see how we can import additional Nuget packages and how we can leverage validation.

Read More

Preview image blogpost

UNO Platform - Build a Todo App - Part 2

This is the second part of our small mini series, where we build a todo app Kanban style with the UNO Platform. In this episode we are laying some ground work as well as defining all of our requirements we want to achieve.

We will slowly start creating our first components and make everything work smoothly together.

Read More

Preview image blogpost

UNO Platform - Build a Todo App - Part 1

This is the beginning of a mini series shedding some lights on the UNO Platform. The UNO Platform enables us to write code once and run it on a wide range of platforms (Windows, MacOS, Android, Linux, ...). So let's have a look at this. To do so we will create a small Todo App Kanban-style so we can organize our thoughts and work 😉

Read More

Preview image blogpost

Using SSE in C# on the example of the sum of a list

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.

Read More

Decorator Pattern using the example of a cached repository

The decorator pattern is a common pattern in object oriented languages. It allows us to add behavior to an individual object without changing the object itself.

We will exactly that with a repository. We introduce a caching layer via the decorator pattern.

Read More

Preview image blogpost

RequiredIf - Extend the validation in Blazor

Just imagine you have your beautiful model in a form. And Blazor is very kind and does validation for you. It is really straight forward because you can annotate your model with some attributes like RequiredAttribute to tell Blazor: "Hey this property has to be set, otherwise the form will not be submitted."

But what if you want to have that easy setup with attributes but also want to say to Blazor: "Hey this property has to be set, if the other property is true.* Well, that does not work out of the box. So let's make it together!

Read More

Preview image blogpost

Directory.Build.props - Centralize your builds

MSBuild has several extensibility hooks to customize your build and today we discover one which let you centralized repeating aspects of your C# projects. Directory.Build.props

Read More

Preview image blogpost

DebuggerTypeProxy - Displaying complex states in the debugger

With the DebuggerTypeProxy we have the possibility to show more complex states. This can be helpful in certain cases where an object for example has a lot of properties.

Read More

Preview image blogpost

Blazor Performance Tuning - Good practices and advanced use cases

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.

Read More

Heap, Stack, Boxing and Unboxing, Performance ... let's order things!

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?

Read More

Preview image blogpost

Prerendering Blazor Apps - How does it work / tips and tricks

Blazor comes with the option to prerender your webpage on the server. This works for the client-side version as well as the server side version of Blazor.

Let's have a look how does it work and what might be some pitfalls and how can we come around those.

Read More

Preview image blogpost

Enum.Equals - Performance analysis

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.

Read More

Preview image blogpost

Lazy load components with Blazor - Virtualize in Action

Since .NET5 we have the ability to "virtualize" a component.

But what exactly is that and how do we use it? This blog post will show you a lot of details about the <Virtualize> tag.

Read More

Preview image blogpost

Generator-Function in C# - What does yield do?

Since the introduction of C# 2.0 we have the yield keyword with in combination with the IEnumerable<T> type works as a generator function. We can return elements one by one.

But how does that thing work internally? And what does it have to do with async / await?

Read More

Preview image blogpost

Task vs ValueTask - The what's, when's and how's

Since .NET Core 2.0 the Type ValueTask. It seems that there is a lot of overlap between Task and ValueTask. So let's have a deeper look into ValueTask. Where should we use it and how should we use it properly? And also: where we should not use is.

Read More

Preview image blogpost

ILGPU - Processing Data on your GPU (CUDA / OpenCL in C#)

If you ever needed a massive amount of data processed in parallel you might had a look at OpenCL or CUDA. Both are based on a subset of C to describe the program your GPU has to execute.

So let's have a look how we can achieve this in the .NET way. Furthermore I'll give you some insights about GPGPU (General-purpose computing on graphics processing units) in general. Also some limitations and big differences to conventional "CPU"-Programming.

Read More

static abstract interfaces and generic math

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.

Read More

Preview image blogpost

A better enumeration - Type safe from start to end

Enumerations are one of the fundamental value types in C#. enum's are handy if you have a constrained set of allowed values. But the enum type has also a lot of drawbacks. Personally one major issue for me, is that you can't define methods inside the enum. Plus you can just pass them invalid values and it just works (Todd Howard I'm looking at you).

So why not make a lightweight alternative which removes a lot of the shortcomings of an enum?

Read More

Preview image blogpost

API Versioning in ASP.NET Core made easy

How can we have versioning with a public facing REST API in ASP.NET Core? This blog post will show you the foundation how to setup and configure versioning. Furthermore I will give you some ideas how to structure your code and what kinds of versioning you can do.

In the end you should be able to have the same endpoints with multiple versions.

Read More

IEnumerable vs IQueryable - What's the difference

.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?

Read More

Tactical Design Patterns in C# - Part 1: Creational

This guide will give an overview of common design patterns in C#. We'll also have a look what is there advantage and how can we implement them. Let's start with creational design patterns.

Read More

Preview image blogpost

Shorts: foreach on type without IEnumerable

Is it possible to have a foreach loop over a type which does not implement IEnumerable? Yes it is.

Read More

Preview image blogpost

Task.WhenAny - How to cancel other tasks

This short post will give an overview how to cancel all remaining tasks once Task.WhenAny finishes. Plus how Task.WhenAny behaves in different circumstances.

Read More

Preview image blogpost

A story about boxing / unboxing and string interpolation in C#

A story about boxing / unboxing and string interpolation in C#. What has string interpolation to do with boxing / unboxing and what's the impact?

Read More

Preview image blogpost

EF Core 6 - New Features

EF Core 6 is right in front of our door and it looks very promising. In this blog post I will show what new features are to come.

Read More

Quickly asked: Should I use unsigned integers (uint, uint8, ...) in C#?

Should I use uint as a type in my library / website / program? For example when iterating through an array? I mean that thing can't be smaller than 0!?

So let's have a look.

Read More

Async Await Pitfalls / Guidelines - .NET User Group Zurich Presentation

On 6th of July I had the honor to present some topics about async/await. Mainly:

  • What is asynchronous programming
  • Deadlocks and ConfigureAwait
  • How does the state machine work
  • Pitfalls and general Guidelines
  • ValueTask

You'll find all the slides and the whole talk in the blog.

Read More

Tutorial Unit and E2E Testing in Blazor - Part 1

This blog post should give you an easy and good introduction how to unit and end-to-end test your Blazor Application. Furthermore it does not matter if we are running server side or client side aka WebAssembly. The main two libraries we are using is first bUnit for unit-testing and Playwright for end-to-end testing. So let's dive in!

Read More

Preview image blogpost

My first Post

Welcome to my blog and my first post

In this article I will show you why I created my own blog-software from scratch and why I have a blog in the first place. Short answer: I like to know how to create a blog and I like blazor. Long answer you will find in the post.

Read More

An error has occurred. This application may no longer respond until reloaded. Reload x