📜  C#|转换类别

📅  最后修改于: 2021-05-29 16:15:57             🧑  作者: Mango

Convert类提供了将基本数据类型转换为另一种基本数据类型的不同方法。 Convert类支持的基本类型为Boolean,Char,SByte,Byte,Int16,Int32,Int64,UInt16,UInt32,UInt64,Single,Double,Decimal,DateTime和String。它还提供了支持其他转换的方法。此类在System名称空间下定义。

转换类的特征:

  • 它提供了用于将每个基本类型转换为每个其他基本类型的方法。
  • 它提供了用于将整数值转换为非十进制字符串表示形式的方法,还可以将表示非十进制数字的字符串转换为整数值。
  • 它提供了用于将任何自定义对象转换为任何基本类型的方法。
  • 它提供了一组支持base64编码的方法。
  • 如果缩小的转换导致数据丢失,则可能发生OverFlowException。

场地:

  • DBNull:这是一个常量,表示缺少数据的数据库列,即数据库null。

方法

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.

范例1:

// C# program to illustrate the
// use of ToBase64String(Byte[])
// method
using 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);
    }
}

输出:

BArray is :02-04-08-10-20

Base 64 string is :AgQIECA=

范例2:

// C# program to illustrate the
// use of ToDecimal(Int16) method
using 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);
        }
    }
}

输出:

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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.convert?view=netframework-4.7.2