Last active
August 6, 2019 17:20
-
-
Save manbeardgames/07cd96f4817e3d02abeb91e94e93a564 to your computer and use it in GitHub Desktop.
A quick example showing the use of a Deconstruct method
This file contains hidden or 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
| using System; | |
| public class Program | |
| { | |
| public static void Main() | |
| { | |
| // Create new FooBar instance | |
| FooBar fBar = new FooBar("Test String", 43); | |
| // This will call the Deconstruct(out string, out int) method of FooBar. | |
| var (foo, bar) = fBar; | |
| // Output to show results. | |
| Console.WriteLine($"Foo: {foo} -- Bar: {bar}"); | |
| Console.ReadLine(); | |
| } | |
| } |
This file contains hidden or 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
| /// <summary> | |
| /// Simple class used to demonstrate the use of a Deconstruct method. | |
| /// </summary> | |
| public class FooBar | |
| { | |
| public string Foo { get; set; } | |
| public int Bar { get; set; } | |
| public FooBar(string foo, int bar) | |
| { | |
| this.Foo = foo; | |
| this.Bar = bar; | |
| } | |
| /// <summary> | |
| /// Deconstructs the FooBar instance | |
| /// </summary> | |
| /// <param name="foo">The Foo</param> | |
| /// <param name="bar">The Bar</param> | |
| public void Deconstruct(out string foo, out int bar) | |
| { | |
| foo = this.Foo; | |
| bar = this.Bar; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment