Skip to content

Instantly share code, notes, and snippets.

@chitoku-k
Created October 23, 2015 16:23
Show Gist options
  • Save chitoku-k/4c90e3e77898c13e8bbd to your computer and use it in GitHub Desktop.
Save chitoku-k/4c90e3e77898c13e8bbd to your computer and use it in GitHub Desktop.
†麻薬†
using System;
/// <summary>
/// <see cref="System.ValueType"/> への拡張メソッドを提供します。
/// </summary>
public static class ValueTypeExtensions
{
/// <summary>
/// 文字列形式を、<see cref="System.Boolean"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static Boolean? ToBoolean(this string s)
{
Boolean value;
return Boolean.TryParse(s, out value) ? value : (Boolean?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.Byte"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static Byte? ToByte(this string s)
{
Byte value;
return Byte.TryParse(s, out value) ? value : (Byte?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.SByte"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static SByte? ToSByte(this string s)
{
SByte value;
return SByte.TryParse(s, out value) ? value : (SByte?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.Char"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static Char? ToChar(this string s)
{
Char value;
return Char.TryParse(s, out value) ? value : (Char?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.Decimal"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static Decimal? ToDecimal(this string s)
{
Decimal value;
return Decimal.TryParse(s, out value) ? value : (Decimal?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.Int16"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static Int16? ToInt16(this string s)
{
Int16 value;
return short.TryParse(s, out value) ? value : (short?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.UInt16"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static UInt16? ToUInt16(this string s)
{
UInt16 value;
return UInt16.TryParse(s, out value) ? value : (UInt16?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.Int32"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static Int32? ToInt32(this string s)
{
Int32 value;
return Int32.TryParse(s, out value) ? value : (Int32?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.UInt32"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static UInt32? ToUInt32(this string s)
{
UInt32 value;
return UInt32.TryParse(s, out value) ? value : (UInt32?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.Int64"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static Int64? ToInt64(this string s)
{
Int64 value;
return Int64.TryParse(s, out value) ? value : (Int64?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.UInt64"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static UInt64? ToUInt64(this string s)
{
UInt64 value;
return UInt64.TryParse(s, out value) ? value : (UInt64?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.Single"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static Single? ToSingle(this string s)
{
Single value;
return Single.TryParse(s, out value) ? value : (Single?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.Double"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static Double? ToDouble(this string s)
{
Double value;
return Double.TryParse(s, out value) ? value : (Double?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.DateTime"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static DateTime? ToDateTime(this string s)
{
DateTime value;
return DateTime.TryParse(s, out value) ? value : (DateTime?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.DateTimeOffset"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static DateTimeOffset? ToDateTimeOffset(this string s)
{
DateTimeOffset value;
return DateTimeOffset.TryParse(s, out value) ? value : (DateTimeOffset?)null;
}
/// <summary>
/// 文字列形式を、<see cref="System.TimeSpan"/> に変換します。変換できない場合は <c>null</c> を返します。
/// </summary>
public static TimeSpan? ToTimeSpan(this string s)
{
TimeSpan value;
return TimeSpan.TryParse(s, out value) ? value : (TimeSpan?)null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment