1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[C#Tips]TimeSpanを拡張メソッドで生成(500.Milliseconds()みたいな感じで)

Last updated at Posted at 2019-01-28

TimeSpanの生成方法

  • 例:500ミリを生成 するとき
    大体以下の2パターン。
            // コンストラクタで生成 => 読み辛い。「この男、素人か。」と思う。
            var ctr500milli = new TimeSpan(0, 0, 0, 0, 500);

            // FromMilliSecondで生成 => 定石
            var fromMilli500 = TimeSpan.FromMilliseconds(500);

自分の方法=> 拡張メソッド を使う。

ただの趣味。。

            // 拡張メソッドを定義して以下のように書く。
            var ext500Milli = 500.Milliseconds();

            // 3.5秒待つ場合は以下のような感じ
            await Task.Delay(3.5.Seconds());
  • 拡張メソッド 定義
    ・ミリ、秒だけじゃなくTicks、時、日と、From{Hoge}メソッドがあるものに対応しています。
    ・Ticksだけは引数がlong。
    ・double/longだけではなく、intにも対応してます。でないと500.0.Milliseconds()とか4l.Ticks()みたいな書き方しないといけないから。
        /// <summary>TimeSpanExtensions</summary>
        public static class TimeSpanExtensions
        {
            /// <summary>To Ticks</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Ticks(this int value) => TimeSpan.FromTicks(value);

            /// <summary>To Ticks</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Ticks(this long value) => TimeSpan.FromTicks(value);

            /// <summary>To Milliseconds</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Milliseconds(this int value) => TimeSpan.FromMilliseconds(value);

            /// <summary>To Milliseconds</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Milliseconds(this double value) => TimeSpan.FromMilliseconds(value);

            /// <summary>To Seconds</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Seconds(this int value) => TimeSpan.FromSeconds(value);

            /// <summary>To Seconds</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Seconds(this double value) => TimeSpan.FromSeconds(value);

            /// <summary>To Minutes</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Minutes(this int value) => TimeSpan.FromMinutes(value);

            /// <summary>To Minutes</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Minutes(this double value) => TimeSpan.FromMinutes(value);

            /// <summary>To Hours</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Hours(this int value) => TimeSpan.FromHours(value);

            /// <summary>To Hours</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Hours(this double value) => TimeSpan.FromHours(value);

            /// <summary>To Days</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Days(this int value) => TimeSpan.FromDays(value);

            /// <summary>To Days</summary>
            /// <param name="value">The value.</param>
            /// <returns>TimeSpan</returns>
            public static TimeSpan Days(this double value) => TimeSpan.FromDays(value);
        }

それでは良いプログラミングライフをノシ

1
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?