Skip to content

Instantly share code, notes, and snippets.

@manbeardgames
Last active August 6, 2019 17:20
Show Gist options
  • Select an option

  • Save manbeardgames/07cd96f4817e3d02abeb91e94e93a564 to your computer and use it in GitHub Desktop.

Select an option

Save manbeardgames/07cd96f4817e3d02abeb91e94e93a564 to your computer and use it in GitHub Desktop.
A quick example showing the use of a Deconstruct method
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();
}
}
/// <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