Created
August 19, 2020 19:37
-
-
Save bergpb/f667d013327d6ff81d9ad9a09fd1f4f5 to your computer and use it in GitHub Desktop.
Exemplo de Extension Methods in C#
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.Globalization | |
| namespace System | |
| { | |
| static class DateTimeExtensions | |
| { | |
| public static string ElapsedTime(this Datime thisObj) | |
| { | |
| TimeSpan duration = DateTime.Now.Subtract(thisObj); | |
| if(duration.TotalHours < 24.0) | |
| { | |
| return duration.TotalHours.ToString("F1", CultureInfo.InvariantCulture) + " hours"; | |
| } | |
| else | |
| { | |
| return duration.TotalDays.ToString("F1", CultureInfo.InvariantCulture) + " days"; | |
| } | |
| } | |
| } | |
| } |
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 ProcessFile | |
| { | |
| public static void Main() | |
| { | |
| DateTime dt = new DateTime(2018, 11, 16, 8, 10, 45); | |
| Console.WriteLine(dt.ElapsedTime()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment