Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert Class in C#
The Convert class in C# provides static methods to convert between different base data types. It offers type-safe conversion methods that can handle null values and format providers, making it more robust than direct casting for many scenarios.
The Convert class includes methods like ToBoolean(), ToDouble(), ToDecimal(), ToInt32(), and many others, each designed to convert values to their respective target types while handling edge cases and cultural formatting.
Convert.ToBoolean() Method
The Convert.ToBoolean() method converts a specified value to an equivalent Boolean value −
Syntax
public static bool ToBoolean(string value, IFormatProvider provider); public static bool ToBoolean(object value);
Example
using System;
using System.Globalization;
public class Demo {
public static void Main() {
CultureInfo culture = new CultureInfo("en-US");
string str = "true";
Console.WriteLine("Converting string to boolean:");
bool result = Convert.ToBoolean(str, culture);
Console.WriteLine("Result: " + result);
// Other examples
Console.WriteLine("Convert.ToBoolean(1): " + Convert.ToBoolean(1));
Console.WriteLine("Convert.ToBoolean(0): " + Convert.ToBoolean(0));
Console.WriteLine("Convert.ToBoolean("false"): " + Convert.ToBoolean("false"));
}
}
The output of the above code is −
Converting string to boolean:
Result: True
Convert.ToBoolean(1): True
Convert.ToBoolean(0): False
Convert.ToBoolean("false"): False
Convert.ToDouble() Method
The Convert.ToDouble() method converts a string representation of a number to an equivalent double-precision floating-point number −
Syntax
public static double ToDouble(string value, IFormatProvider provider); public static double ToDouble(object value);
Example
using System;
using System.Globalization;
public class Demo {
public static void Main() {
string val = "876876.878";
NumberFormatInfo formatProvider = new NumberFormatInfo();
formatProvider.NumberDecimalSeparator = ".";
Console.WriteLine("Converting string to double:");
double result = Convert.ToDouble(val, formatProvider);
Console.WriteLine("Result: " + result);
// Converting from different types
Console.WriteLine("Convert.ToDouble(123): " + Convert.ToDouble(123));
Console.WriteLine("Convert.ToDouble(true): " + Convert.ToDouble(true));
}
}
The output of the above code is −
Converting string to double: Result: 876876.878 Convert.ToDouble(123): 123 Convert.ToDouble(true): 1
Convert.ToDecimal() Method
The Convert.ToDecimal() method converts a string representation of a number to an equivalent decimal number −
Syntax
public static decimal ToDecimal(string value, IFormatProvider provider); public static decimal ToDecimal(object value);
Example
using System;
using System.Globalization;
public class Demo {
public static void Main() {
CultureInfo culture = new CultureInfo("en-US");
string val = "8787.50";
Console.WriteLine("Converting string to decimal:");
decimal result = Convert.ToDecimal(val, culture);
Console.WriteLine("Result: " + result);
// Converting from different types
Console.WriteLine("Convert.ToDecimal(123.45f): " + Convert.ToDecimal(123.45f));
Console.WriteLine("Convert.ToDecimal(999): " + Convert.ToDecimal(999));
}
}
The output of the above code is −
Converting string to decimal: Result: 8787.50 Convert.ToDecimal(123.45f): 123.45 Convert.ToDecimal(999): 999
Common Convert Methods
| Method | Description | Example |
|---|---|---|
| ToInt32() | Converts to 32-bit integer | Convert.ToInt32("123") |
| ToString() | Converts to string representation | Convert.ToString(123) |
| ToChar() | Converts to Unicode character | Convert.ToChar(65) |
| ToDateTime() | Converts to DateTime object | Convert.ToDateTime("2023-01-01") |
Conclusion
The Convert class in C# provides a comprehensive set of methods for type conversion with built-in error handling and culture-specific formatting support. It is particularly useful when converting between different data types safely, especially when dealing with user input or data from external sources that may contain null values.
