C# | Convert Class - GeeksforGeeks

03 Apr, 2019
Convert class 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 System namespace.
Characteristics of Convert class:
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, also convert the string represent the non-decimal numbers to integer values.
It provides methods that are used to convert any custom object to any base type.
It provide a set of methods that supports base64 encoding.
An OverFlowException can occur if a narrowing conversion results in a loss of data.
Field:
DBNull: It is a constant which represents a database column that is absent of data i.e. database null.
Example 1:
// C# program to illustrate the// use of ToBase64String(Byte[])// methodusing System;  class GFG {      // Main method    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 str = Convert.ToBase64String(B);          // Display the string        Console.WriteLine("Base 64 string is :{0}", str);    }}
Output:
BArray is :02-04-08-10-20

Base 64 string is :AgQIECA=

Example 2:
// C# program to illustrate the// use of ToDecimal(Int16) methodusing System;  class GFG {      // Main method    static public void Main()    {          // Creating and initializing         // an array        short[] ele = {1, Int16.MinValue,                         -00, 106, -32 };        decimal sol;          // Display the elements        Console.WriteLine("Elements are:");        foreach(short i in ele)        {            Console.WriteLine(i);        }          foreach(short num in ele)        {              // Convert the given Int16            // values into decimal values            // using ToDecimal(Int16) method            sol = Convert.ToDecimal(num);              // Display the elements            Console.WriteLine("convert value is: {0}", sol);        }    }}
Output:
Elements are:
1
-32768
0
106
-32
convert value is: 1
convert value is: -32768
convert value is: 0
convert value is: 106
convert value is: -32

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.convert?view=netframework-4.7.2
CSharp Convert Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
Difference between Ref and Out keywords in C#
Introduction to .NET Framework
C# | Delegates
C# | String.IndexOf( ) Method | Set - 1
C# | Abstract Classes
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
Different ways to sort an array in descending order in C#