Created
August 6, 2019 17:40
-
-
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
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() | |
| { | |
| // 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(); | |
| } | |
| } |
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> | |
| /// 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