usingSystem;usingSystem.Linq;usingSystem.Collections.Generic;// https://dev.to/jonhilt/wtf-is-a-lambda-2dg2publicclassPerson{publicstringName{get;set;}publicintAge{get;set;}// Allow Object Deconstructuring/destructuring// https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct#deconstructing-a-user-defined-type-with-discardspublicvoidDeconstruct(outstringname,outintage){name=Name;age=Age;}publicoverridestringToString(){return$"Name={this.Name}, Age={this.Age}";}}publicclassProgram{staticList<Person>people=newList<Person>{newPerson{Name="Bob",Age=37},newPerson{Name="Brian",Age=21},newPerson{Name="Amy",Age=58}};publicstaticvoidMain(){// This works// var bob = people.SingleOrDefault(person => person.Name == "Bob");// But this doesn't// var bob = people.SingleOrDefault(({name}) => name == "Bob");varbob=people.SingleOrDefault(((Name,_))=>Name=="Bob");Console.WriteLine(bob);}}
And Roslyn 2.0 compiler complains...
}
Compilation error (line 39, col 48): Invalid expression term '=>'
Compilation error (line 39, col 48): Syntax error, ',' expected
Compilation error (line 39, col 51): Syntax error, ',' expected
Compilation error (line 39, col 38): The type or namespace name 'Name' could not be found (are you missing a using directive or an assembly reference?)
Compilation error (line 39, col 44): The type or namespace name '_' could not be found (are you missing a using directive or an assembly reference?)
Compilation error (line 39, col 37): Predefined type'System.ValueTuple`2' is not defined or imported
Compilation error (line 39, col 51): The name 'Name' does not exist in the current context
I wasn't able to replicate the object desconstruction in C# as you could do in JavaScript (even after implementing Deconstruct)
dotnetfiddle.net/Wbx3ac
And Roslyn 2.0 compiler complains...
Sorry, I was talking explicitly on JS terms
No worries, mate~ 😉