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:
using SafeConvert;
Convert from string "1" to byte
var b = "1".ToByte(); // Print 1
Convert from string "10" to short
var s = "10".ToShort(); // Print 10
Convert from string "100" to int
var n = "100".ToInt(); // Print 100
Convert from string "1000" to long
var l = "1000".ToLong(); // Print 1000
Convert from string "6.5" to float
var f = "6.5".ToFloat(); // Print 6.5
Convert from string "6.5" to decimal
var d = "6.5".ToDecimal(); // Print 6.5
Convert from string "6.5" to double
var d = "6.5".ToDouble(); // Print 6.5
Convert from string "2014-12-02 11:00:00" to DateTime
var dateTime = "2014-12-02 11:00:00".ToDateTime();
Convert from string "20" to int using generic
var n = "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
var n = "abc".ToInt(10); // Print default value 10 because of failing to parse
This library has been tested using NUnit Test Framework. If any issue or bug, please push a new issue here.
Copyright 2015 by CongDongDotNet - MIT License