The SafeConvert is a .NET library used to convert data between data types safely.
In order to use this library, your application needs the .NET Framework 3.5 or higher. If okay, you can install via two following ways:
- Via Nuget:
Install-Package SafeConvert - Via Github:
git clone https://github.com/congdongdotnet/SafeConvert.git
After refering to the SafeConvert library, remember that you need to include the library by using keyword:
usingSafeConvert;Convert from string "1" to byte
varb="1".ToByte();// Print 1Convert from string "10" to short
vars="10".ToShort();// Print 10Convert from string "100" to int
varn="100".ToInt();// Print 100Convert from string "1000" to long
varl="1000".ToLong();// Print 1000Convert from string "6.5" to float
varf="6.5".ToFloat();// Print 6.5Convert from string "6.5" to decimal
vard="6.5".ToDecimal();// Print 6.5Convert from string "6.5" to double
vard="6.5".ToDouble();// Print 6.5Convert from string "2014-12-02 11:00:00" to DateTime
vardateTime="2014-12-02 11:00:00".ToDateTime();Convert from string "20" to int using generic
varn="20".To<int>();Each extension methods has the default value in case of failing to parse. E.g: convert from string "abc" to int using default value
varn="abc".ToInt(10);// Print default value 10 because of failing to parseThis library has been tested 
Copyright 2015 by CongDongDotNet - MIT License
