CSharp · Console
using System;
using System.Linq;
using System.Collections.Generic;
// https://dev.to/jonhilt/wtf-is-a-lambda-2dg2
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
// Allow Object Deconstructuring/destructuring
// https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct#deconstructing-a-user-defined-type-with-discards
public void Deconstruct(out string name, out int age) {
name = Name;
age = Age;
}
public override string ToString() {
return $"Name={this.Name}, Age={this.Age}";
}
}
public class Program
{
static List<Person> people = new List<Person>
{
new Person { Name = "Bob", Age = 37 },
new Person { Name = "Brian", Age = 21 },
new Person { Name = "Amy", Age = 58 }
};
public static void Main()
{
// This works
// var bob = people.SingleOrDefault(person => person.Name == "Bob");
// But this doesn't
// var bob = people.SingleOrDefault(({name}) => name == "Bob");
var bob = people.SingleOrDefault(((Name, _)) => Name == "Bob");
Console.WriteLine(bob);
}
}