Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the name of this syntax in C# (multiple tuple-like assignment)?

Tags:

c#

I have found this syntax in MS docs:

public NewsStoryViewModel(DateTimeOffset published, string title, string uri) =>
        (Published, Title, Uri) = (published, title, uri);

It does multiple assignment in one statement, but I am not quite sure what it's called or how it works.

Is this some kind of a trick do multiple assignment by pretending these are tuples or does it have it's own name?

P.S. It is mentioned here if anyone is interested

P.P.S. It's not the absence of {} I am wondering about, but rather line 2 with a "fancy" assignment instead of a traditional one.

like image 548
Ilya Chernomordik Avatar asked Dec 04 '22 17:12

Ilya Chernomordik


1 Answers

It's a tuple that is deconstructed to class properties.

public NewsStoryViewModel(DateTimeOffset published, string title, string uri) {
    // tuple
    var myTuple = (published, title, uri);
    // deconstruction
    (Published, Title, Uri) = myTuple;
}

Deconstruction works for tuples out of the box. See https://learn.microsoft.com/en-us/dotnet/csharp/tuples#deconstruction for details.

like image 152
Christoph Lütjen Avatar answered Jun 03 '23 05:06

Christoph Lütjen