Can I make Json.net deserialize a C# 9 record type with the “primary” constructor, as if it had [JsonConstructor]?

Firstly, you only have to do this when you create your own constructors. This is due to the fact that on instantiation it won’t know which one to use. Secondly, note that (by default) the deserializer will use the property and constructor names and overwrite the ones you omit in the actual constructor type. Furthermore, … Read more

Init + private set accessors on the same property?

Similar to specifying a constructor to initialize your value, you can use a private backing field so that you can still take advantage of the init logic and allow initialization without a specific constructor public record Stuff { private int _myProperty; public int MyProperty { get => _myProperty; init => _myProperty = value; } public … Read more

Why is an explicit `this` constructor initializer required in records with a primary constructor?

This is because primary constructor parameters are a little bit special – they are in scope throughout initialization of the record. Guess what the following program prints: System.Console.WriteLine(new Foo(Bar: 42).Baz); public record Foo(int Bar) { public int Bar => 41; public int Baz = Bar; } 41 or 42? And the answer is… drumroll please… … Read more

How to enable C# 9.0-preview

As of October 2020: Please see @Pac0’s answer here: https://stackoverflow.com/a/64386529/159145 As of June 2020: According to this page in the documentation you need to edit your *.csproj to set the <LangVersion> to preview. Also mentioned in the blog-post about the preview-release, but not the above documentation page, is that you need to update your project’s … Read more

Can I use C# 9 records as IOptions?

Is there any way to configure the IOptions configuration system to deserialize the options using the record’s constructor instead of requiring a parameterless constructor? No, in general ASP.Net Core uses a lot of run-time type instancing, which requires constructor calls that are known beforehand, and in this case it requires a constructor with no arguments. … Read more

How to use C# 9 records with EF Core?

From official document Entity Framework Core depends on reference equality to ensure that it uses only one instance of an entity type for what is conceptually one entity. For this reason, record types aren’t appropriate for use as entity types in Entity Framework Core. This could confuse some people. Pay close attention to the documentation. … Read more

Record type with multiple constructors

Simply add the constructor you want like this: record Rank(int level, string description); record Manager(string FirstName, Rank Rank) { public Manager() : this(“”, new Rank(0, “Entry”)) { } // public Manager(string FirstName, Rank Rank) is auto generated }

How do I target attributes for a record class?

To target the various parts of the expanded class, use the appropriate attribute target. For instance: // Target the property, use `property` record Person(string FirstName, string LastName, [property: JsonIgnore] int Age); // Target the backing field of the property, use `field` record Person(string FirstName, string LastName, [field: JsonIgnore] int Age); // Target the constructor parameter, … Read more

Predefined type ‘System.Runtime.CompilerServices.IsExternalInit’ is not defined or imported [duplicate]

This is a small bug in Visual Studio 2019 that hasn’t been fixed yet. To solve this, you need to add a dummy class named IsExternalInit with the namespace System.Runtime.CompilerServices anywhere in your project. That will do it. If writing a library it’s best to make this class internal, as otherwise you can end up … Read more