Shouldn't positional properties of records be get-only instead of init-only? #4282
-
If I declare a record like this: public record Foo(string Name); Then the following code is legal: var f = new Foo("Hello") { Name = "WAT?" }; The Sure, it's not a very big issue, I could just "not do this". But it's annoying, because when you have both positional properties and "normal" properties, Intellisense shows properties that have already been initialized in the constructor. For instance, if I add a non-positional property to my previous example: public record Foo(string Name)
{
public string Description { get; init; }
} Then intellisense shows this (this is in LinqPad, but Visual Studio exhibits the same behavior): I realize it's probably too late to fix that, since C# 9 has shipped, but I wonder if this is something that had been considered during the design phase? If it can't be fixed in the language, maybe VS could exclude positional properties when suggesting properties for object initializer syntax (the same way it excludes already initialized properties). What do you think? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Positional records have record Person(string First, string Last);
var person1 = new Person("John", "Smith");
var person2 = person1 with { Last = "Jones" };
// compiles into
var person2 = person1.<>Clone();
person2.Last = "Jones"; |
Beta Was this translation helpful? Give feedback.
Positional records have
init
property accessors to supportwith
: