Questions tagged [c#-9.0]

Use this tag for issues related to development with version 9.0 of the C# language. You should generally also include the [c#] tag.

In Build 2020 Mads Torgersen announced the following features :

  • Init-Only properties
  • Records
  • Top-Level programs
  • Pattern-matching improvements
  • Improved target typing
  • Covariant returns

The Language Feature Status page in GitHub tracks the implementation status of C# 9 features.

264 questions
305
votes
4 answers

When to use record vs class vs struct

Should I be using Record for all of my DTO classes that move data between controller and service layer? Should I be using Record for all my request bindings since ideally I would want the request sent to the controller to be immutable for my…
frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
78
votes
2 answers

Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported

I have been having this issues while testing the new features of C# 9.0 with Visual Studio 2019 Preview. I was testing the init setter, but the compiler shows error with the message: Error CS0518 Predefined type…
Kevin
  • 785
  • 1
  • 4
  • 7
70
votes
4 answers

Testing C# 9.0 in VS2019 - CS0518 IsExternalInit is not defined or imported ... How do I define/import it?

EDIT [Nov 29 2020]: .NET 5.0 is out now, but the solution below is still required if you're targetting .NET Standard 2.1 C# 9.0 is still under development. There are a couple references which lead me to believe it should be testable now (some of…
Josh
  • 2,958
  • 3
  • 16
  • 27
59
votes
2 answers

How to copy/clone records in C# 9?

The C# 9 records feature specification includes the following: A record type contains two copying members: A constructor taking a single argument of the record type. It is referred to as a "copy constructor". A synthesized public parameterless…
mbabramo
  • 2,573
  • 2
  • 20
  • 24
51
votes
8 answers

C# Source Generator - warning CS8032: An instance of analyzer cannot be created

I'm trying to build a Source Generator. Right now, just the most basic static method that returns "Hello World". The generator project builds, but the generated code is not available, the debugger never starts, and the build output shows CSC :…
farlee2121
  • 2,959
  • 4
  • 29
  • 41
51
votes
1 answer

Custom Equality check for C# 9 records

From what I understand, records are actually classes that implement their own equality check in a way that your object is value-driven and not reference driven. In short, for the record Foo that is implemented like so: var foo = new Foo { Value =…
panosru
  • 2,709
  • 4
  • 33
  • 39
49
votes
4 answers

record types with collection properties & collections with value semantics

In c# 9, we now (finally) have record types: public record SomeRecord(int SomeInt, string SomeString); This gives us goodies like value semantics: var r1 = new SomeRecord(0, "zero"); var r2 = new SomeRecord(0, "zero"); Console.WriteLine(r1 == r2);…
jeroenh
  • 26,362
  • 10
  • 73
  • 104
46
votes
6 answers

C# 9 records validation

With the new record type of C# 9, how is it possible to inject a custom parameter validation/ null check/ etc during the construction of the object without having to re-write the entire constructor? Something similar to this: record Person(Guid Id,…
Simon Mattes
  • 4,866
  • 2
  • 33
  • 53
41
votes
2 answers

How do I target attributes for a record class?

When defining a record class, how do I target the attributes to the parameter, field or property? For instance, I would like to use JsonIgnore but this doesn't compile as it has an attribute usage restriction to the field or property: record…
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
40
votes
6 answers

Record type with multiple constructors

How do I create multiple constructors for a record type in C#? I created a record type like this: public record Person(int Id, string FirstName, string LastName) Now I want to introduce another constructor overload with no parameters, how can I do…
Nadar
  • 1,521
  • 2
  • 11
  • 21
36
votes
2 answers

What is the difference between "is not null" and "!= null"?

With the release of C# 9.0, the negated null constant pattern was introduced. The documentation for pattern matching states: Beginning with C# 9.0, you can use a negated null constant pattern to check for non-null, as the following example…
Pawel
  • 900
  • 2
  • 10
  • 19
34
votes
3 answers

How to use C# 9 records with EF Core?

I am using ASP.NET Core 5 Web API and I am trying to use the new C# records as my model classes. But I am getting an EF Core error about tracking problems whenever I update my modified model using the with…
Phil K
  • 4,939
  • 6
  • 31
  • 56
34
votes
3 answers

Can I use C# 9 records as IOptions?

I have just started playing around with C# 9 and .NET 5.0, specifically the new record construct. I find I have a lot of excellent use cases for the shorthand syntax of the record types. One of the use cases I've considered was using a record for…
Nikolaj Dam Larsen
  • 5,455
  • 4
  • 32
  • 45
32
votes
4 answers

How to enable C# 9.0-preview

I have downloaded and installed v5.0.0-preview.5. My project is targeting net5.0 but C# 9.0 is not working. How can I enable C# 9.0?
Jerzy Grzelec
  • 341
  • 1
  • 3
  • 6
29
votes
3 answers

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

In C# 9 we can create positional records causing them to get a constructor, which the spec draft calls a primary constructor. We can create a custom constructor as well, but as stated in the spec: If a record has a primary constructor, any…
V0ldek
  • 9,623
  • 1
  • 26
  • 57
1
2 3
17 18