top of page

C# Convert Class

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.


Syntax

Following is the syntax −

public static bool ToBoolean (string val, IFormatProvider provider);

Val is a string that contains the value of either TrueString or FalseString, whereas the provider is an object that supplies culture-specific formatting information.


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.



Example 1: Convert.ToDouble() Method

using System;
using System.Globalization;
public class Demo {
   public static void Main(){
      String val = "876876, 878";
      NumberFormatInfo formatProvider = new NumberFormatInfo();
      formatProvider.NumberDecimalSeparator = ", ";
      formatProvider.NumberGroupSeparator = ".";
      formatProvider.NumberGroupSizes = new int[] { 2 };
      Console.WriteLine("Converted Decimal value is :");
      double res = Convert.ToDouble(val, formatProvider);
      Console.Write("{0}", res);
   }
}

Output:

Example 2: Convert.ToDecimal() Method

using System;
using System.Globalization;
public class Demo {
   public static void Main(){
      CultureInfo cultures = new CultureInfo("en-US");
      String val = "8787";
      Console.WriteLine("Converted Decimal value is :");
      decimal res = Convert.ToDecimal(val, cultures);
      Console.Write("{0}", res);
   }
}

Output:


C# Convert Class 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 subset of a Unicode character array, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array. Parameters specify the subset in the input array 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 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.



The Tech Platform



0 comments
bottom of page