Global usings in C# 10

One of the new features in .NET 6 is support for global using declarations. We can define using declarations as global and they work all over the project so we don’t have to define them in other files anymore. Here’s how it works.

Cost of exceptions

Hopefully the era of leprosy and corona is over for this time and it’s time to get back to blogging. Exceptions are powerful feature of object-oriented languages as far as they are used like they are thought to use – throw exception only when something really unexpected happens. This advice should be taken seriously – here’s why.

Top-level programs in C# 9.0

C# 9.0 comes with nice new feature called top-level programs. It’s something that teachers of beginner classes will love for sure. Imagine – you start teaching C# with only two lines of code on screen. All missing code is generated by compiler. This blog post introduces top-level programs and shows some secrets of new C# compiler.

Behind the compiler: 20 examples of C# code before and after compiling

Over years I have written many blog posts about C# and .NET that demonstrate also how things work internally and what C# compiler produces from the code we write. I have called these chapters usually as “Behind the compiler”. This post is growing list of my writings covering interesting findings about C# compiler work.

Using nameof operator in C#

There’s one very useful and often overlooked operator in C#. It’s called nameof and its purpose is to return name of something. Although it is operator we can live without it may still commit to coding by pointing out some errors at compile time. Here’s how to use the nameof operator in practice.

All about IDisposable

So, what is IDisposable and what is disposing of objects? This is something I teach to developers classes before they are going to their first school practice at some company. One of important concepts is disposing. Here’s my story about it.

Using Roslyn to build object to object mapper

Back in time I wrote series of posts about how I built simple object to object mapper. It was nine years ago and from them more things have changed. Today I added one new implementation of mapper and this one is using Roslyn compiler services to generate dynamic code for mappings.

C# 8: Default implementations in interfaces

C# 8.0 will introduce new language feature – default implementations of interface members. It means that we can define body to interface member and implementing class that doesn’t implement given interface member will use default one from interface itself. Here’s my deep-dive and analyzis to default implementions of interfaces.

Serializing objects to URL encoded form data

While messing with dictionaries to create form data for FormUrlEncodedContent so I can send data to server using HTTP client, I started thinking about easier and cleaner way to do it. I was writing integration tests and I wanted to re-use some model classes instead of dictionaries. Here’s how to do it. Sample of integration test is incluced.

Optimized hierarchy traverser

My first draft of hierarchy traversing component got some serious feedback and it’s time to make some changes before moving on to next challenges. Hierarchy traverser is not optimal yet as it uses tail-call recursion and it’s easy to run to stack overflow with it. This blog post solves this problem and prepares for next challenges like node cache and continue-from-given-node.

Universal hierarchy traversing in C#

I started playing with small idea about how to go through document repository on SharePoint using more universal approach than just piling code to using-blocks and methods that depend on these. My goal was to separate in code hierarchy traversing logic from document exporting logic so I can use traversing part also in other projects on different types of hirarchies. Here is my nice and clean solution.

Breaking static dependency

Static dependency can be nightmare for developers who write tests for their code. There is not much to do to get rid of static dependencies if they come with third-party libraries or NuGet packages. This blog post introduces two tricks to make code with static dependencies testable.

Using-declarations in C# 8.0

One of new language features of C# 8.0 is support for using declarations. These declarations enable shorter syntax for declaring disposable variables we want to dispose. Also using declarations give us a little bit cleaner code while compiler makes a dirty work of producing correct code that takes care of disposing disposable variables.

String repeat method for C#

C# doesn’t have built-in function to repeat a string. There are different version for string.Repeat() available in internet and it’s up to reader to find out which version works better. Here is the list of most popular implementations I found across the web. I list my findings here with the results of simple performance test.

Adding attribute to backing field of automated property

There are scenarios where developers want to add attributes to backing field of automated property. It was hard to do before C# 7.3. Now it is supported in Visual Studio and solution is simple. This blog post shows how to add attribute to backing field of automated property.

Pattern matching in switch statements

One of new features introduced by C# 7.0 is support for pattern matching in switch statements. It’s like mix of switch and if statements so we don’t have to nest these two. This blog post introduces pattern matching in switch statements and shows what C# compiler produces of switch statements.

Non-trailing named arguments in C# 7.2

One small change that comes with C# 7.2 is support for non-trailing named arguments in method calls. This post explains what are non-trailing named arguments, how to use them and how they look after compiling.

protected private access modifier in C# 7.2

C# 7.2 introduces new access modifier – protected private. It targets developers who are responsible for class libraries and API-s design and who need also consistent design for internals of class libraries. This blog post shows how protected private access modifier works.

Default literal expressions in C# 7.1

C# 7.1 introduces a little update to default literal expressions that makes them a little bit shorter and on some cases helps us write cleaner and more readable code. This blog post introduces this litlle feature update in C# 7.1.

Inferred tuple names in C# 7.1

Although C# 7.1 hasn’t many new features there are still some convenience hacks I like. One of these is inferred tuple names meaning that we can name tuple members using variable names. It’s not a big change in language but it still makes code a little bit cleaner where tuples are used.

Ref returns and ref locals in C# 7.0

C# 7.0 introduces ref returns and ref locals. Main goal of these new features is to make it easier for developers to pass around references to value types instead of copies of their values. This is important when working with large data structures that are implemented as value types. This blog post shows how to use ref returns and ref locals in Ć#.