Last active
December 7, 2016 19:07
-
-
Save elderfo/d3b398a00d433841da0efd3dcfe057e4 to your computer and use it in GitHub Desktop.
Destructuring in JavaScript vs Deconstruction C#7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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