Converting Strings to .NET Objects – IParsable and ISpanParsable

Converting Strings to .NET Objects – IParsable and ISpanParsable

A new feature of C# 11 allows abstract static members with interfaces. This makes it possible to define class methods to be used as a contract with a generic class implementation, e.g. using + and – operators. With .NET 7, numeric types implement many new interfaces. This C# 11 feature is not only about math! The new IParsable and ISpanParsable interfaces allow creating objects from strings. As these interfaces can be used with constraints in generic types, parsing strings to create objects is now an easy task with generic implementations. This article shows implementing both the string and the Span version of the parse interfaces and using them with generic types. Continue reading Converting Strings to .NET Objects – IParsable and ISpanParsable

Primary Constructors with C#

Primary Constructors with C#

To reduce the syntax needed when writing C# code, C# 9 added records with primary constructors. C# 10 then added records for structs. Using C# 12 you can create classes with primary constructors. This article explains the *primary constructor* syntax and shows the differences between class records, struct records, and normal classes. Continue reading Primary Constructors with C#

<strong>What’s the SynchronizationContext used for?</strong>

What’s the SynchronizationContext used for?

Creating Windows applications, UI controls are bound to the UI thread. .NET made it to different iterations with different patterns dealing with asynchronous programming. .NET 4.0 introduced the Task Parallel Library (TPL) and C# 5 added the async and await keywords. Together with these enhancements of .NET, and the synchronization context, invoking methods that make use of different threads has becoming a lot easier. Continue reading What’s the SynchronizationContext used for?

Upgrading an ASP.NET Core Web API Project to .NET 6

Upgrading an ASP.NET Core Web API Project to .NET 6

Upgrading an ASP.NET Core 5 application to .NET 6, all what needs to be done is to change the project file for .NET 6, and update the NuGet packages to the new versions, and you’re done and can build and run the application. However, to take advantage of new features, and reduce the number of source code lines, some things can be changed – as shown in this article. Continue reading Upgrading an ASP.NET Core Web API Project to .NET 6

C# 8 Updates

C# 8 Updates

Bonus chapter for Professional C# 7 and .NET Core 2 with updates for C# 8: nullable reference types, using declaration, readonly members, default interface members, async streams, switch expressions, pattern matching enhancements, indices and ranges, and more! Continue reading C# 8 Updates

How Hello World! changed – top level statements and functions (C# 9)

How Hello World! changed – top level statements and functions (C# 9)

C# 9 comes with many new features to enhance productivity and fix bugs. One productivity enhancement comes for small and simple programs also helps learning C#: top level statements. This also allows for a new way to create a “Hello World” program with C#. It’s not the first time that a new C# language feature made a change for “Hello World”. This also happened with C# 6. Let’s come on a tour how “Hello World” changed during the C# years, and what’s going on with top level statements and functions. Continue reading How Hello World! changed – top level statements and functions (C# 9)