One cool feature with C# record, is the ability to deconstruct them based on their default constructor without having to define your own Deconstruct method.
public record Foo(int Bar, int Baz);
public void Func(Foo foo) {
var (bar, baz) = foo;
}
However, for some reason this does not seem to work when the default constructor contains only a single parameter.
public record Foo(int Bar) {
public int? Baz { get; init; }
}
public void Func(Foo foo) {
var (bar) = foo;
}
When trying to deconstruct a record with only one parameter in its default constructor, it produces the build errors [CS1003] Syntax error, ',' expected
and [CS1001] Identifier expected
. I may recall that this is also the case when defining your own Deconstruct method with only one out
parameter. I am just curious as to why this is the case.