Strings are converted to a numeric value according to the following rules:
From | To | On success | On failure |
---|---|---|---|
string | int/uint | Numeric value | 0 |
string | int?/uint? | Numeric value | null |
string | short | Numeric value | 0 |
string | short? | Numeric value | null |
string | long/ulong | Numeric value | 0 |
string | long?/ulong? | Numeric value | null |
string | decimal | Numeric value. Both '.' and ',' are treated as decimal separator | 0 |
string | decimal? | Numeric value. Both '.' and ',' are treated as decimal separator | null |
string | float/double | Numeric value. Both '.' and ',' are treated as decimal separator | 0 |
string | float?/double? | Numeric value. Both '.' and ',' are treated as decimal separator | null |
Examples:
String | Convert to | Result |
---|---|---|
"1" | int | 1 |
"5.4" | decimal | 5.4 |
"5,4" | decimal | 5.4 |
null | decimal | 0 |
"" | decimal | 0 |
Converting strings to DateTime values is a little more complicated, but the base .NET Framework does a pretty good job, so the Vici conversion is similar to the built-in stuff (but a little easier to use).
To convert a string to a DateTime, you should tell the converter what date formats should be expected:
DateTime? dt1 = s.Convert<DateTime?>("MM/dd/yyyy HH:mm:ss","yyyy-MM-dd HH:mm:ss.FFFFF");
If you always use the same date formats, you can set this as the default:
StringConverter.RegisterDateFormats("MM/dd/yyyy HH:mm:ss","yyyy-MM-dd HH:mm:ss.FFFFF"); DateTime? dt1 = s.Convert<DateTime?>();
From | To | On success | On failure |
---|---|---|---|
string | DateTime | Date value | DateTime.MinValue |
string | DateTime? | Date value | null |
The Iridium Converter can convert numeric and named values to enum values.
For example:
enum LogLevel { Debug = 0, Information = 1, Error = 2, FatalError = 3 }; "1".Convert<LogLevel>(); // returns LogLevel.Information "3".Convert<LogLevel>(); // returns LogLevel.FatalError "Error".Convert<LogLevel>(); // return LogLevel.Error
If a value can't be converted, the default is returned, which is the value 0 (zero) casted to the enum type (according to the C# specs). If the target type is nullable and the conversion fails, //null// is returned.
For example:
enum LogLevel { Debug = 0, Information = 1, Error = 2, FatalError = 3 }; "5".Convert<LogLevel>(); // returns LogLevel.Debug "Bogus".Convert<LogLevel>(); // returns LogLevel.Debug "5".Convert<LogLevel?>(); // returns null "Bogus".Convert<LogLevel?>(); // returns null
If you want to have your own default value when conversion fails, you should use the nullable conversion and use the ?? operator:
enum LogLevel { Debug = 0, Information = 1, Error = 2, FatalError = 3 }; LogLevel logLevel = "5".Convert<LogLevel?>() ?? LogLevel.Information; LogLevel logLevel = "Bogus".Convert<LogLevel?>() ?? LogLevel.Information;
Conversion from string to boolean is done according to fixed true and false string values:
String value | boolean value |
---|---|
"1" | true |
"T" or "T" | true |
"Y" or "y" | true |
"True" or "true" or "True" | true |
"Yes" or "YES" or "yes" | true |
anything else | false |