Skip to content

Instantly share code, notes, and snippets.

@elderfo
Last active December 7, 2016 19:07
Show Gist options
  • Save elderfo/d3b398a00d433841da0efd3dcfe057e4 to your computer and use it in GitHub Desktop.
Save elderfo/d3b398a00d433841da0efd3dcfe057e4 to your computer and use it in GitHub Desktop.
Destructuring in JavaScript vs Deconstruction C#7
// Requires references to:
// - System.ValueTuple
// - Microsoft.VisualStudio.QualityTools.UnitTestFramework
public class Person
{
public string Name { get; set; }
public string City { get; set; }
public void Deconstruct(out string name, out string city)
{
(name, city) = (Name, City);
}
public void Deconstruct(out string name)
{
name = Name;
}
}
[TestMethod]
public void Can_Deconstruct()
{
var obj = new Person
{
Name = "Chris",
City = "Omaha"
};
var (name) = obj;
Assert.AreEqual(obj.Name, name);
var (name2, city) = obj;
Assert.AreEqual(obj.Name, name2);
Assert.AreEqual(obj.City, city);
}
var obj = {
Name : 'Chris',
City: 'Omaha'
}
var {Name} = obj;
console.assert(Name === obj.Name);
// OR
var {Name, City} = obj;
console.assert(Name === obj.Name);
console.assert(City === obj.City);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment