Skip to content

Instantly share code, notes, and snippets.

@manbeardgames
Created August 6, 2019 17:40
Show Gist options
  • Select an option

  • Save manbeardgames/073693f5880258878fb1e53a9c3ba2e7 to your computer and use it in GitHub Desktop.

Select an option

Save manbeardgames/073693f5880258878fb1e53a9c3ba2e7 to your computer and use it in GitHub Desktop.
A quick example of showing how to add a Deconstruct method using Extensions for existing built-in types
using System;
public class Program
{
public static void Main()
{
// Calls the Deconstruct(DateTime, int, int, int) method we created in
// the DateTimeExtensions class.
var (day, month, year) = DateTime.Now;
// Output to show results.
Console.WriteLine($"{day}/{month}/{year}");
Console.ReadLine();
}
}
/// <summary>
/// Extension class for DateTime objects that adds a Deconstruct method for DateTime
/// </summary>
public static class DateTimeExtensions
{
/// <summary>
/// Deconstructs a DateTime instance into day, month, and year ints.
/// </summary>
/// <param name="dateTime">The DateTime instance</param>
/// <param name="day">The day</param>
/// <param name="month">The month</param>
/// <param name="year">The year</param>
public static void Deconstruct(this DateTime dateTime, out int day, out int month, out int year)
{
day = dateTime.Day;
month = dateTime.Month;
year = dateTime.Year;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment