Created
March 7, 2014 11:04
-
-
Save baba-s/9409588 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Linq; | |
using System.Text; | |
using System.Collections.Generic; | |
using System.Globalization; | |
public static partial class StringExtensions | |
{ | |
public static sbyte ToSByte(this string s) | |
{ | |
return sbyte.Parse(s); | |
} | |
public static sbyte? ToSByteOrNull(this string s) | |
{ | |
sbyte result; | |
if(sbyte.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static sbyte ToSByteOrDefault(this string s, sbyte defaultValue = default(sbyte)) | |
{ | |
sbyte result; | |
if(sbyte.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static byte ToByte(this string s) | |
{ | |
return byte.Parse(s); | |
} | |
public static byte? ToByteOrNull(this string s) | |
{ | |
byte result; | |
if(byte.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static byte ToByteOrDefault(this string s, byte defaultValue = default(byte)) | |
{ | |
byte result; | |
if(byte.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static char ToChar(this string s) | |
{ | |
return char.Parse(s); | |
} | |
public static char? ToCharOrNull(this string s) | |
{ | |
char result; | |
if(char.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static char ToCharOrDefault(this string s, char defaultValue = default(char)) | |
{ | |
char result; | |
if(char.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static short ToShort(this string s) | |
{ | |
return short.Parse(s); | |
} | |
public static short? ToShortOrNull(this string s) | |
{ | |
short result; | |
if(short.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static short ToShortOrDefault(this string s, short defaultValue = default(short)) | |
{ | |
short result; | |
if(short.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static ushort ToUShort(this string s) | |
{ | |
return ushort.Parse(s); | |
} | |
public static ushort? ToUShortOrNull(this string s) | |
{ | |
ushort result; | |
if(ushort.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static ushort ToUShortOrDefault(this string s, ushort defaultValue = default(ushort)) | |
{ | |
ushort result; | |
if(ushort.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static int ToInt(this string s) | |
{ | |
return int.Parse(s); | |
} | |
public static int? ToIntOrNull(this string s) | |
{ | |
int result; | |
if(int.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static int ToIntOrDefault(this string s, int defaultValue = default(int)) | |
{ | |
int result; | |
if(int.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static uint ToUInt(this string s) | |
{ | |
return uint.Parse(s); | |
} | |
public static uint? ToUIntOrNull(this string s) | |
{ | |
uint result; | |
if(uint.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static uint ToUIntOrDefault(this string s, uint defaultValue = default(uint)) | |
{ | |
uint result; | |
if(uint.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static long ToLong(this string s) | |
{ | |
return long.Parse(s); | |
} | |
public static long? ToLongOrNull(this string s) | |
{ | |
long result; | |
if(long.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static long ToLongOrDefault(this string s, long defaultValue = default(long)) | |
{ | |
long result; | |
if(long.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static ulong ToULong(this string s) | |
{ | |
return ulong.Parse(s); | |
} | |
public static ulong? ToULongOrNull(this string s) | |
{ | |
ulong result; | |
if(ulong.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static ulong ToULongOrDefault(this string s, ulong defaultValue = default(ulong)) | |
{ | |
ulong result; | |
if(ulong.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static float ToFloat(this string s) | |
{ | |
return float.Parse(s); | |
} | |
public static float? ToFloatOrNull(this string s) | |
{ | |
float result; | |
if(float.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static float ToFloatOrDefault(this string s, float defaultValue = default(float)) | |
{ | |
float result; | |
if(float.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static double ToDouble(this string s) | |
{ | |
return double.Parse(s); | |
} | |
public static double? ToDoubleOrNull(this string s) | |
{ | |
double result; | |
if(double.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static double ToDoubleOrDefault(this string s, double defaultValue = default(double)) | |
{ | |
double result; | |
if(double.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static decimal ToDecimal(this string s) | |
{ | |
return decimal.Parse(s); | |
} | |
public static decimal? ToDecimalOrNull(this string s) | |
{ | |
decimal result; | |
if(decimal.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static decimal ToDecimalOrDefault(this string s, decimal defaultValue = default(decimal)) | |
{ | |
decimal result; | |
if(decimal.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static DateTime ToDateTime(this string s) | |
{ | |
return DateTime.Parse(s); | |
} | |
public static DateTime? ToDateTimeOrNull(this string s) | |
{ | |
DateTime result; | |
if(DateTime.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static DateTime ToDateTimeOrDefault(this string s, DateTime defaultValue = default(DateTime)) | |
{ | |
DateTime result; | |
if(DateTime.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
public static bool ToBoolean(this string s) | |
{ | |
return bool.Parse(s); | |
} | |
public static bool? ToBooleanOrNull(this string s) | |
{ | |
bool result; | |
if(bool.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return null; | |
} | |
public static bool ToBooleanOrDefault(this string s, bool defaultValue = default(bool)) | |
{ | |
bool result; | |
if(bool.TryParse(s,out result)) | |
{ | |
return result; | |
} | |
return defaultValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment