6 less popular facts about C# 9 records

csharp9_01

A lot of C# 9-related content is around. Very often, records are mentioned as one of the most interestning new features. So, while we can find A LOT of buzz around them, I wanted to provide a distilled set of facts typically not presented when describing them.

Fact #1. You can use them in pre-.NET 5

Records has been announced as C# 9 feature (and thus .NET 5), and it is the officially supported way. But you can “not officialy” use most C# 9 features in earlier frameworks, as they don’t need the new runtime support. So, if being not “officially supported” does not bother you too much, just set proper LangVersion in csproj and you are (almost) done:

Trying to compile super typical example like the following:

will still not compile, complaining about the lack of mysterious IsExternalInit type:

To be funny, the workaround is just to define it in your project (exactly as it is in the newer CoreLib, shipped with .NET 5):

BTW, IsExternalInit is not required for the record usage by itself, but for init as discussed in https://github.com/dotnet/runtime/issues/34978 and https://github.com/dotnet/runtime/pull/37763. So if creating mutable records is ok, no need for that.

Sidenote: If you are interested what more you can “not officially” use, look at Using C# 9 outside .NET 5 #47701 discussion.

Fact #2. with-expression clones them

This fact has been mentioned here and there but it is worth repeating: with-expression is a shallow copy of an orignal instance with some properties overwritten. Thus, when writting:

It is translated into three steps: allocation, shallow copy and some properties overwritten:

because <Clone>$() is just:

That’s why it is much better (or at least, expected) that records are immutable. If not, shallow copies may be misleading when operating on more complex data structures.

Fact #3. Records can be generic and use constraints

It is much rarely mentioned that records can be generic:

And then the underlying class handles it perfectly. For example using the default .ToString behaviour for T Data field in case of ToString implementation:

and default equality behaviour for it:

Moreover, the same applies to positional records syntax:

Fact #4. Records can implement interfaces

As records are just classes underneath, they can derive from other classes and – which is showed a little less often – implement intefaces:

and again, the same applies to positional records syntax:

Fact #5. Records can be partial

Another interesting fact, records can be also partial:

So, mostly, this gives us Source Generators possibility.

Fact #6. Records can use attributes

Another not mentioned, but sometimes desired property, may be possibility to add atributes, as in regular class:

Although, to make it working for positional records, you need to add some attribute-voodoo property::

Moreover, as Marc Gravell pointed on Twitter, with this voodoo, you can apply attributes both to the property itself and to the backing field:

And, obviously, you can apply your own attributes, to create really sophisticated code:

Last words…

Although, in summary nothing super surprising is happenning here – as we can use on records most of the syntax that is possible for classes – I found it interesting to confirm what is indeed possible or not. Anything more to add? Any ideas how we can utilize those possibilites? 🙂

5 comments

  1. The title “You can use them in pre-C# 9” is a bit confusing, as you can’t use records in previous C# versions. What you meant to say is that you can use them in previous *.net runtime* versions (net core 3, & .net standard and I suspect any netfx version as well) using the approach you describe.

Leave a Reply

Your email address will not be published. Required fields are marked *