0
2

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# 便利関数 int等のparseの拡張関数

Last updated at Posted at 2018-05-23

C#でstringをint等にparseするのに標準だと少し長いし、outや例外の処理が一手間かかるので拡張関数を定義することにしました。

標準の関数

public static int Parse(
	string s
)
public static bool TryParse(
	string s,
	out int result
)

拡張関数版(独自定義)


        /// <summary>
        /// int値文字列をint値に変換
        /// </summary>
        public static int ToInt(this string str)
        {
            int value = int.Parse(str);
            return value;
        }

        /// <summary>
        /// int値文字列をint値に変換
        /// </summary>
        public static int ToIntOrDefault(this string str)
        {
            int value;
            int.TryParse(str, out value);
            return value;
        }

        /// <summary>
        /// int値文字列をint値に変換
        /// </summary>
        public static int ToIntOrDefault(this string str, int defaultValue)
        {
            int value;
            if (int.TryParse(str, out value))
            {
                return value;
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// long値文字列をlong値に変換
        /// </summary>
        public static long ToLong(this string str)
        {
            long value = long.Parse(str);
            return value;
        }

        /// <summary>
        /// long値文字列をlong値に変換
        /// </summary>
        public static long ToLongOrDefault(this string str)
        {
            long value;
            long.TryParse(str, out value);
            return value;
        }

        /// <summary>
        /// long値文字列をlong値に変換
        /// </summary>
        public static long ToLongOrDefault(this string str, long defaultValue)
        {
            long value;
            if (long.TryParse(str, out value))
            {
                return value;
            }
            else
            {
                return defaultValue;
            }
        }


        /// <summary>
        /// decimal値文字列をdecimal値に変換
        /// </summary>
        public static decimal ToDecimal(this string str)
        {
            decimal value = decimal.Parse(str);
            return value;
        }

        /// <summary>
        /// decimal値文字列をdecimal値に変換
        /// </summary>
        public static decimal ToDecimalOrDefault(this string str)
        {
            decimal value;
            decimal.TryParse(str, out value);
            return value;
        }

        /// <summary>
        /// decimal値文字列をdecimal値に変換
        /// </summary>
        public static decimal ToDecimalOrDefault(this string str, decimal defaultValue)
        {
            decimal value;
            if (decimal.TryParse(str, out value))
            {
                return value;
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 日付をParseできたときはその日付を返し、Parseできなかったときはdefault日付を返す
        /// </summary>
        public static DateTime ParseDateTimeOrDefault(
            string s, string format, DateTime defaultDateTime)
        {
            DateTime resultOut;
            var ret = DateTime.TryParseExact(s, format,
                System.Globalization.CultureInfo.InvariantCulture,
                System.Globalization.DateTimeStyles.None,
                out resultOut);
            if (ret)
            {
                defaultDateTime = resultOut;
            }
            return defaultDateTime;
        }

public static class Utilなどのクラスに上記を定義することで使えるようになります。
(名前から内容が想像できないクラス名を使うのはアンチパターンですが。)

0
2
0

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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?