Convert class in C# provides different methods to convert a base data type to another base data type. The base types supported by the Convert class are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime, and String. It also provides methods that support other conversions. This class is defined under the System namespace. There are some Important Characteristics mentioned below.
- It provides methods that are used to convert every base type into every other base type.
- It provides methods that are used to convert integer values to the non-decimal string representation, and also convert the string representing the non-decimal numbers to integer values.
- It provides methods that are used to convert any custom object to any base type.
- It provides a set of methods that supports base64 encoding.
- An OverFlowException can occur if a narrowing conversion results in a loss of data.
- DBNull: It is a constant that represents a database column that is absent of data i.e. database null
Example: C# program to convert String to Integer Using Convert Class.
C#
// C# program to converting String to Integer
// Using Convert Class
using System;
public class Geeks
{
static public void Main () {
String Strnum = "123";
// Converts string to integer
int num = Convert.ToInt32(Strnum);
Console.WriteLine(Strnum+ " Coverted to Integer: "+
num );
}
}
Output123 Coverted to Integer: 123
Methods
Method | Description |
---|
ChangeType() | It returns an object of a specified type whose value is equivalent to a specified object. |
---|
FromBase64CharArray(Char[], Int32, Int32) | Converts a portion of a Unicode character array, which encodes binary data as base-64, into an equivalent 8-bit unsigned integer array. Parameters define the subset and the number of elements to convert. |
---|
FromBase64String(String) | Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array. |
---|
GetTypeCode(Object) | Returns the TypeCode for the specified object. |
---|
IsDBNull(Object) | Returns an indication of whether the specified object is of type DBNull. |
---|
ToBase64CharArray() | Converts a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array encoded with base-64 digits. |
---|
ToBase64String() | Converts the value of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits. |
---|
ToBoolean() | Converts a specified value to an equivalent Boolean value. |
---|
ToByte() | Converts a specified value to an 8-bit unsigned integer. |
---|
ToChar() | Converts a specified value to a Unicode character. |
---|
ToDateTime() | Converts a specified value to a DateTime value. |
---|
ToDecimal() | Converts a specified value to a decimal number. |
---|
ToDouble() | Converts a specified value to a double-precision floating-point number. |
---|
ToInt16() | Converts a specified value to a 16-bit signed integer. |
---|
ToInt32() | Converts a specified value to a 32-bit signed integer. |
---|
ToInt64() | Converts a specified value to a 64-bit signed integer. |
---|
ToSByte() | Converts a specified value to an 8-bit signed integer |
---|
ToSingle() | Converts a specified value to a single-precision floating-point number. |
---|
ToUInt16() | Converts a specified value to a 16-bit unsigned integer. |
---|
ToUInt32() | Converts a specified value to a 32-bit unsigned integer. |
---|
ToUInt64() | Converts a specified value to a 64-bit unsigned integer. |
---|
Example 1:
C#
// C# program to illustrate the
// use of ToBase64String(Byte[]) method
using System;
class Geeks
{
static public void Main()
{
// Creating and initializing
// Byte array
byte[] b = { 2, 4, 8, 16, 32 };
// Display the elements
Console.WriteLine("BArray is :{0}",
BitConverter.ToString(b));
Console.WriteLine();
// Convert the given array
// into a base 64 string
String s = Convert.ToBase64String(b);
Console.WriteLine("Base 64 string is :{0}", s);
}
}
OutputBArray is :02-04-08-10-20
Base 64 string is :AgQIECA=
Example 2:
C#
// C# program to illustrate the
// use of ToDecimal(Int16) method
using System;
class Geeks
{
static public void Main()
{
// Creating and initializing
// an array
short[] e = {1, Int16.MinValue,
-00, 106, -32 };
decimal d;
// Display the elements
Console.WriteLine("Elements are:");
foreach(short i in e)
{
Console.WriteLine(i);
}
foreach(short num in e)
{
// Convert the given Int16
// values into decimal values
// using ToDecimal(Int16) method
d = Convert.ToDecimal(num);
Console.WriteLine("converted value is: {0}", d);
}
}
}
OutputElements are:
1
-32768
0
106
-32
converted value is: 1
converted value is: -32768
converted value is: 0
converted value is: 106
converted value is: -32