다음을 통해 공유


.NET의 형식 변환

모든 값에는 값에 할당된 공간의 양, 값에 사용할 수 있는 가능한 값 범위 및 사용할 수 있는 멤버와 같은 특성을 정의하는 연결된 형식이 있습니다. 많은 값을 둘 이상의 형식으로 표현할 수 있습니다. 예를 들어 값 4는 정수 또는 부동 소수점 값으로 표현할 수 있습니다. 형식 변환은 이전 형식의 값과 동일하지만 원래 개체의 ID(또는 정확한 값)를 반드시 유지하지는 않는 새 형식의 값을 만듭니다.

.NET은 다음 변환을 자동으로 지원합니다.

  • 파생 클래스에서 기본 클래스로 변환합니다. 예를 들어 클래스 또는 구조체의 인스턴스를 Object 인스턴스로 변환할 수 있습니다. 이 변환에는 캐스팅 또는 변환 연산자가 필요하지 않습니다.

  • 기본 클래스에서 원래 파생 클래스로 다시 변환합니다. C#에서 이 변환에는 캐스팅 연산자가 필요합니다. Visual Basic에서는 CType 있는 경우 Option Strict 연산자가 필요합니다.

  • 인터페이스를 구현하는 형식에서 해당 인터페이스를 나타내는 인터페이스 개체로 변환합니다. 이 변환에는 캐스팅 또는 변환 연산자가 필요하지 않습니다.

  • 인터페이스 개체에서 해당 인터페이스를 구현하는 원래 형식으로 다시 변환합니다. C#에서 이 변환에는 캐스팅 연산자가 필요합니다. Visual Basic에서는 CType 있는 경우 Option Strict 연산자가 필요합니다.

이러한 자동 변환 외에도 .NET은 사용자 지정 형식 변환을 지원하는 몇 가지 기능을 제공합니다. 여기에는 다음이 포함되었습니다.

  • 형식 간의 사용 가능한 확대 변환을 정의하는 Implicit 연산자입니다. 자세한 내용은 암시적 연산자 섹션을 사용하여 암시적 변환을 참조하세요.

  • 형식 간의 사용 가능한 축소 변환을 정의하는 Explicit 연산자입니다. 자세한 내용은 명시적 연산자 섹션을 사용하여 명시적 변환을 참조하세요.

  • 각 기본 .NET 데이터 형식에 대한 변환을 정의하는 IConvertible 인터페이스입니다. 자세한 내용은 IConvertible Interface 섹션을 참조하세요.

  • Convert 인터페이스에서 메서드를 구현하는 메서드 집합을 제공하는 IConvertible 클래스입니다. 자세한 내용은 클래스 변환 섹션을 참조하세요.

  • 지정된 형식을 다른 형식으로 변환할 수 있도록 확장할 수 있는 기본 클래스인 TypeConverter 클래스입니다. 자세한 내용은 TypeConverter 클래스 섹션을 참조하세요.

암시적 연산자를 사용한 암시적 변환

확대 변환에는 대상 형식보다 더 제한적인 범위 또는 더 제한된 멤버 목록이 있는 기존 형식의 값에서 새 값을 만드는 작업이 포함됩니다. 변환이 확대되면 데이터가 손실될 수 없습니다(정밀도 손실이 발생할 수 있음). 데이터를 손실할 수 없으므로 컴파일러는 명시적 변환 메서드 또는 캐스팅 연산자를 사용하지 않고도 변환을 암시적 또는 투명하게 처리할 수 있습니다.

비고

암시적 변환을 수행하는 코드는 변환 메서드를 호출하거나 캐스팅 연산자를 사용할 수 있지만 암시적 변환을 지원하는 컴파일러에서는 이 메서드를 사용할 필요가 없습니다.

예를 들어 Decimal 형식은 Byte, Char, Int16, Int32, Int64, SByte, UInt16, UInt32UInt64 값에서 암시적 변환을 지원합니다. 다음 예제에서는 Decimal 변수에 값을 할당할 때 이러한 암시적 변환 중 일부를 보여 줍니다.

  byte byteValue = 16;
  short shortValue = -1024;
  int intValue = -1034000;
  long longValue = 1152921504606846976;
  ulong ulongValue = UInt64.MaxValue;

  decimal decimalValue;

  decimalValue = byteValue;
  Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                    byteValue.GetType().Name, decimalValue);

  decimalValue = shortValue;
  Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                    shortValue.GetType().Name, decimalValue);

  decimalValue = intValue;
  Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                    intValue.GetType().Name, decimalValue);

  decimalValue = longValue;
  Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                    longValue.GetType().Name, decimalValue);

decimalValue = ulongValue;
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                  ulongValue.GetType().Name, decimalValue);
// The example displays the following output:
//    After assigning a Byte value, the Decimal value is 16.
//    After assigning a Int16 value, the Decimal value is -1024.
//    After assigning a Int32 value, the Decimal value is -1034000.
//    After assigning a Int64 value, the Decimal value is 1152921504606846976.
//    After assigning a UInt64 value, the Decimal value is 18446744073709551615.
Dim byteValue As Byte = 16
Dim shortValue As Short = -1024
Dim intValue As Integer = -1034000
Dim longValue As Long = CLng(1024 ^ 6)
Dim ulongValue As ULong = ULong.MaxValue

Dim decimalValue As Decimal

decimalValue = byteValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                  byteValue.GetType().Name, decimalValue)

decimalValue = shortValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                  shortValue.GetType().Name, decimalValue)

decimalValue = intValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                  intValue.GetType().Name, decimalValue)

decimalValue = longValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                  longValue.GetType().Name, decimalValue)

decimalValue = ulongValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
                  ulongValue.GetType().Name, decimalValue)
' The example displays the following output:
'    After assigning a Byte value, the Decimal value is 16.
'    After assigning a Int16 value, the Decimal value is -1024.
'    After assigning a Int32 value, the Decimal value is -1034000.
'    After assigning a Int64 value, the Decimal value is 1152921504606846976.
'    After assigning a UInt64 value, the Decimal value is 18446744073709551615.

특정 언어 컴파일러가 사용자 지정 연산자를 지원하는 경우 사용자 고유의 사용자 지정 형식으로 암시적 변환을 정의할 수도 있습니다. 다음 예제는 부호-크기 표현 방식을 사용하는 ByteWithSign라는 이름의 부호 있는 바이트 데이터 유형의 부분 구현을 제공합니다. ByteSByte 값을 ByteWithSign 값으로의 암시적 변환을 지원합니다.

public struct ByteWithSign
{
    private SByte signValue;
    private Byte value;

    public static implicit operator ByteWithSign(SByte value)
    {
        ByteWithSign newValue;
        newValue.signValue = (SByte)Math.Sign(value);
        newValue.value = (byte)Math.Abs(value);
        return newValue;
    }

    public static implicit operator ByteWithSign(Byte value)
    {
        ByteWithSign newValue;
        newValue.signValue = 1;
        newValue.value = value;
        return newValue;
    }

    public override string ToString()
    {
        return (signValue * value).ToString();
    }
}
Public Structure ImplicitByteWithSign
    Private signValue As SByte
    Private value As Byte

    Public Overloads Shared Widening Operator CType(value As SByte) As ImplicitByteWithSign
        Dim newValue As ImplicitByteWithSign
        newValue.signValue = CSByte(Math.Sign(value))
        newValue.value = CByte(Math.Abs(value))
        Return newValue
    End Operator

    Public Overloads Shared Widening Operator CType(value As Byte) As ImplicitByteWithSign
        Dim NewValue As ImplicitByteWithSign
        newValue.signValue = 1
        newValue.value = value
        Return newValue
    End Operator

    Public Overrides Function ToString() As String
        Return (signValue * value).ToString()
    End Function
End Structure

클라이언트 코드는 다음 예제처럼 명시적 변환이나 캐스팅 연산자를 사용하지 않고 ByteWithSign 변수를 선언한 후 ByteSByte 값을 할당할 수 있습니다.

SByte sbyteValue = -120;
ByteWithSign value = sbyteValue;
Console.WriteLine(value);
value = Byte.MaxValue;
Console.WriteLine(value);
// The example displays the following output:
//       -120
//       255
Dim sbyteValue As SByte = -120
Dim value As ImplicitByteWithSign = sbyteValue
Console.WriteLine(value.ToString())
value = Byte.MaxValue
Console.WriteLine(value.ToString())
' The example displays the following output:
'       -120
'       255

명시적 연산자를 사용하여 명시적 변환

축소 변환에는 대상 형식보다 범위가 크거나 멤버 목록이 큰 기존 형식의 값에서 새 값을 만드는 작업이 포함됩니다. 축소 변환으로 인해 데이터가 손실될 수 있으므로 컴파일러는 변환 메서드 또는 캐스팅 연산자를 호출하여 변환을 명시적으로 수행해야 하는 경우가 많습니다. 즉, 변환은 개발자 코드에서 명시적으로 처리되어야 합니다.

비고

변환 범위를 좁히기 위해 변환 방법 또는 캐스팅 연산자를 요구하는 주요 목적은 개발자가 코드에서 처리할 수 있도록 데이터 손실 또는 OverflowException 가능성을 인식하도록 하는 것입니다. 그러나 일부 컴파일러는 이 요구 사항을 완화할 수 있습니다. 예를 들어 Visual Basic에서 Option Strict 꺼져 있는 경우(기본 설정) Visual Basic 컴파일러는 축소 변환을 암시적으로 수행하려고 합니다.

예를 들어 UInt32, Int64UInt64 데이터 형식에는 다음 표와 같이 Int32 데이터 형식을 초과하는 범위가 있습니다.

유형 Int32 범위와 비교
Int64 Int64.MaxValue Int32.MaxValue보다 크고 Int64.MinValueInt32.MinValue보다 작습니다(음수 범위가 더 큼).
UInt32 UInt32.MaxValueInt32.MaxValue보다 큽니다.
UInt64 UInt64.MaxValueInt32.MaxValue보다 큽니다.

이러한 축소 변환을 처리하기 위해 .NET에서는 형식이 Explicit 연산자를 정의할 수 있습니다. 개별 언어 컴파일러는 고유한 구문을 사용하여 이 연산자를 구현하거나 변환을 수행하기 위해 Convert 클래스의 멤버를 호출할 수 있습니다. (Convert 클래스에 대한 자세한 내용은 이 항목의 뒷부분에 있는 Convert 클래스 참조하세요.) 다음 예제에서는 언어 기능을 사용하여 잠재적으로 범위를 벗어난 정수 값을 Int32 값으로 명시적으로 변환하는 방법을 보여 줍니다.

long number1 = int.MaxValue + 20L;
uint number2 = int.MaxValue - 1000;
ulong number3 = int.MaxValue;

int intNumber;

try
{
    intNumber = checked((int)number1);
    Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
                      number1.GetType().Name, intNumber);
}
catch (OverflowException)
{
    if (number1 > int.MaxValue)
        Console.WriteLine($"Conversion failed: {number1} exceeds {int.MaxValue}.");
    else
        Console.WriteLine($"Conversion failed: {number1} is less than {int.MinValue}.");
}

try
{
    intNumber = checked((int)number2);
    Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
                      number2.GetType().Name, intNumber);
}
catch (OverflowException)
{
    Console.WriteLine($"Conversion failed: {number2} exceeds {int.MaxValue}.");
}

try
{
    intNumber = checked((int)number3);
    Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
                      number3.GetType().Name, intNumber);
}
catch (OverflowException)
{
    Console.WriteLine($"Conversion failed: {number1} exceeds {int.MaxValue}.");
}

// The example displays the following output:
//    Conversion failed: 2147483667 exceeds 2147483647.
//    After assigning a UInt32 value, the Integer value is 2147482647.
//    After assigning a UInt64 value, the Integer value is 2147483647.
Dim number1 As Long = Integer.MaxValue + 20L
Dim number2 As UInteger = Integer.MaxValue - 1000
Dim number3 As ULong = Integer.MaxValue

Dim intNumber As Integer

Try
    intNumber = CInt(number1)
    Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
                        number1.GetType().Name, intNumber)
Catch e As OverflowException
    If number1 > Integer.MaxValue Then
        Console.WriteLine("Conversion failed: {0} exceeds {1}.",
                                          number1, Integer.MaxValue)
    Else
        Console.WriteLine("Conversion failed: {0} is less than {1}.\n",
                                          number1, Integer.MinValue)
    End If
End Try

Try
    intNumber = CInt(number2)
    Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
                        number2.GetType().Name, intNumber)
Catch e As OverflowException
    Console.WriteLine("Conversion failed: {0} exceeds {1}.",
                                      number2, Integer.MaxValue)
End Try

Try
    intNumber = CInt(number3)
    Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
                        number3.GetType().Name, intNumber)
Catch e As OverflowException
    Console.WriteLine("Conversion failed: {0} exceeds {1}.",
                                      number1, Integer.MaxValue)
End Try
' The example displays the following output:
'    Conversion failed: 2147483667 exceeds 2147483647.
'    After assigning a UInt32 value, the Integer value is 2147482647.
'    After assigning a UInt64 value, the Integer value is 2147483647.

명시적 변환은 언어마다 다른 결과를 생성할 수 있으며, 이러한 결과는 해당 Convert 메서드에서 반환되는 값과 다를 수 있습니다. 예를 들어 Double 값 12.63251이 Int32변환되는 경우 Visual Basic CInt 메서드와 .NET Convert.ToInt32(Double) 메서드는 모두 Double 반올림하여 값을 13으로 반환하지만 C# (int) 연산자는 12 값을 반환하기 위해 Double 자른다. 마찬가지로 C# (int) 연산자는 부울-정수 변환을 지원하지 않지만 Visual Basic CInt 메서드는 true 값을 -1로 변환합니다. 반면에 Convert.ToInt32(Boolean) 메서드는 true 값을 1로 변환합니다.

대부분의 컴파일러에서는 명시적 변환을 선택되거나 선택되지 않은 방식으로 수행할 수 있습니다. 확인된 변환이 수행되면 변환할 형식의 값이 대상 형식 범위를 벗어나면 예외 OverflowException이 발생합니다. 같은 조건에서 확인되지 않은 변환이 수행되면 변환에서 예외가 발생하지 않을 수 있으나 정확한 동작이 정의되지 않아 잘못된 결과가 나올 수 있습니다.

비고

C#에서는 캐스팅 연산자와 함께 checked 키워드를 사용하거나 /checked+ 컴파일러 옵션을 지정하여 확인된 변환을 수행할 수 있습니다. 반대로 선택되지 않은 변환은 캐스팅 연산자와 함께 unchecked 키워드를 사용하거나 /checked- 컴파일러 옵션을 지정하여 수행할 수 있습니다. 기본적으로 명시적 변환은 선택되지 않습니다. Visual Basic에서는 프로젝트의 고급 컴파일러 설정 대화 상자에서 정수 오버플로 제거 확인 확인란을 선택 취소하거나 /removeintchecks- 컴파일러 옵션을 지정하여 선택한 변환을 수행할 수 있습니다. 반대로, 프로젝트의 고급 컴파일러 설정 대화 상자에서 정수 오버플로 제거 확인 확인란을 선택하거나 /removeintchecks+ 컴파일러 옵션을 지정하여 선택되지 않은 변환을 수행할 수 있습니다. 기본적으로 명시적 변환이 선택됩니다.

다음 C# 예제에서는 checkedunchecked 키워드를 사용하여 Byte 범위를 벗어난 값이 Byte변환되는 경우 동작의 차이를 보여 줍니다. 검사된 변환은 예외를 발생시키지만, 비검사 변환은 Byte.MaxValueByte 변수에 할당합니다.

int largeValue = Int32.MaxValue;
byte newValue;

try
{
    newValue = unchecked((byte)largeValue);
    Console.WriteLine($"Converted the {largeValue.GetType().Name} value {largeValue} to the {newValue.GetType().Name} value {newValue}.");
}
catch (OverflowException)
{
    Console.WriteLine($"{largeValue} is outside the range of the Byte data type.");
}

try
{
    newValue = checked((byte)largeValue);
    Console.WriteLine($"Converted the {largeValue.GetType().Name} value {largeValue} to the {newValue.GetType().Name} value {newValue}.");
}
catch (OverflowException)
{
    Console.WriteLine($"{largeValue} is outside the range of the Byte data type.");
}
// The example displays the following output:
//    Converted the Int32 value 2147483647 to the Byte value 255.
//    2147483647 is outside the range of the Byte data type.

특정 언어 컴파일러가 사용자 지정 오버로드된 연산자를 지원하는 경우 사용자 고유의 사용자 지정 형식으로 명시적 변환을 정의할 수도 있습니다. 다음 예제는 부호-크기 표현 방식을 사용하는 ByteWithSign라는 이름의 부호 있는 바이트 데이터 유형의 부분 구현을 제공합니다. Int32UInt32 값을 ByteWithSign 값으로의 명시적 변환을 지원합니다.

public struct ByteWithSignE
{
    private SByte signValue;
    private Byte value;

    private const byte MaxValue = byte.MaxValue;
    private const int MinValue = -1 * byte.MaxValue;

    public static explicit operator ByteWithSignE(int value)
    {
        // Check for overflow.
        if (value > ByteWithSignE.MaxValue || value < ByteWithSignE.MinValue)
            throw new OverflowException(String.Format("'{0}' is out of range of the ByteWithSignE data type.",
                                                      value));

        ByteWithSignE newValue;
        newValue.signValue = (SByte)Math.Sign(value);
        newValue.value = (byte)Math.Abs(value);
        return newValue;
    }

    public static explicit operator ByteWithSignE(uint value)
    {
        if (value > ByteWithSignE.MaxValue)
            throw new OverflowException(String.Format("'{0}' is out of range of the ByteWithSignE data type.",
                                                      value));

        ByteWithSignE newValue;
        newValue.signValue = 1;
        newValue.value = (byte)value;
        return newValue;
    }

    public override string ToString()
    {
        return (signValue * value).ToString();
    }
}
Public Structure ByteWithSign
    Private signValue As SByte
    Private value As Byte

    Private Const MaxValue As Byte = Byte.MaxValue
    Private Const MinValue As Integer = -1 * Byte.MaxValue

    Public Overloads Shared Narrowing Operator CType(value As Integer) As ByteWithSign
        ' Check for overflow.
        If value > ByteWithSign.MaxValue Or value < ByteWithSign.MinValue Then
            Throw New OverflowException(String.Format("'{0}' is out of range of the ByteWithSign data type.", value))
        End If

        Dim newValue As ByteWithSign

        newValue.signValue = CSByte(Math.Sign(value))
        newValue.value = CByte(Math.Abs(value))
        Return newValue
    End Operator

    Public Overloads Shared Narrowing Operator CType(value As UInteger) As ByteWithSign
        If value > ByteWithSign.MaxValue Then
            Throw New OverflowException(String.Format("'{0}' is out of range of the ByteWithSign data type.", value))
        End If

        Dim NewValue As ByteWithSign

        newValue.signValue = 1
        newValue.value = CByte(value)
        Return newValue
    End Operator

    Public Overrides Function ToString() As String
        Return (signValue * value).ToString()
    End Function
End Structure

클라이언트 코드는 다음 예제와 같이 ByteWithSign 변수를 선언하고, 할당에 캐스팅 연산자 또는 변환 메서드가 포함되는 경우 Int32UInt32 값을 할당할 수 있습니다.

ByteWithSignE value;

try
{
    int intValue = -120;
    value = (ByteWithSignE)intValue;
    Console.WriteLine(value);
}
catch (OverflowException e)
{
    Console.WriteLine(e.Message);
}

try
{
    uint uintValue = 1024;
    value = (ByteWithSignE)uintValue;
    Console.WriteLine(value);
}
catch (OverflowException e)
{
    Console.WriteLine(e.Message);
}
// The example displays the following output:
//       -120
//       '1024' is out of range of the ByteWithSignE data type.
Dim value As ByteWithSign

Try
    Dim intValue As Integer = -120
    value = CType(intValue, ByteWithSign)
    Console.WriteLine(value)
Catch e As OverflowException
    Console.WriteLine(e.Message)
End Try

Try
    Dim uintValue As UInteger = 1024
    value = CType(uintValue, ByteWithSign)
    Console.WriteLine(value)
Catch e As OverflowException
    Console.WriteLine(e.Message)
End Try
' The example displays the following output:
'       -120
'       '1024' is out of range of the ByteWithSign data type.

IConvertible 인터페이스

모든 형식을 공용 언어 런타임 기본 형식으로 변환할 수 있도록 .NET은 IConvertible 인터페이스를 제공합니다. 구현 형식은 다음을 제공하는 데 필요합니다.

  • 구현 형식의 TypeCode 반환하는 메서드입니다.

  • 구현 형식을 각 공용 언어 런타임 기본 형식(Boolean, Byte, DateTime, Decimal, Double등)으로 변환하는 메서드입니다.

  • 구현 형식의 인스턴스를 지정된 다른 형식으로 변환하는 일반화된 변환 방법입니다. 지원되지 않는 변환은 InvalidCastException을 발생시켜야 합니다.

각 공용 언어 런타임 기본 형식(즉, Boolean, Byte, Char, DateTime, Decimal, Double, Int16, Int32, Int64, SByte, Single, String, UInt16, UInt32UInt64)과 DBNullEnum 형식은 물론 IConvertible 인터페이스를 구현합니다. 그러나 명시적 인터페이스 구현은 다음과 같습니다. 변환 메서드는 다음 예제와 같이 IConvertible 인터페이스 변수를 통해서만 호출할 수 있습니다. 다음은 Int32 값을 해당 Char 값으로 변환하는 예제입니다.

int codePoint = 1067;
IConvertible iConv = codePoint;
char ch = iConv.ToChar(null);
Console.WriteLine($"Converted {codePoint} to {ch}.");
Dim codePoint As Integer = 1067
Dim iConv As IConvertible = codePoint
Dim ch As Char = iConv.ToChar(Nothing)
Console.WriteLine("Converted {0} to {1}.", codePoint, ch)

구현 형식이 아닌 인터페이스에서 변환 메서드를 호출해야 하므로 명시적 인터페이스 구현에 상대적으로 비용이 많이 듭니다. 대신 Convert 클래스의 적절한 멤버를 호출하여 공용 언어 런타임 기본 형식 간에 변환하는 것이 좋습니다. 자세한 내용은 다음 섹션 Convert 클래스참조하세요.

비고

IConvertible 인터페이스 및 .NET에서 제공하는 Convert 클래스 외에도 개별 언어는 변환을 수행하는 방법을 제공할 수 있습니다. 예를 들어 C#에서는 캐스팅 연산자를 사용합니다. Visual Basic은 CType, CIntDirectCast같은 컴파일러 구현 변환 함수를 사용합니다.

대부분의 경우 IConvertible 인터페이스는 .NET의 기본 형식 간 변환을 지원하도록 설계되었습니다. 그러나 해당 형식을 다른 사용자 지정 형식으로 변환할 수 있도록 사용자 지정 형식에서 인터페이스를 구현할 수도 있습니다. 자세한 내용은 이 항목의 뒷부분에 있는 ChangeType 메서드 사용자 지정 변환 섹션을 참조하세요.

해당 Convert 클래스

각 기본 형식의 IConvertible 인터페이스 구현을 호출하여 형식 변환을 수행할 수 있지만 System.Convert 클래스의 메서드를 호출하는 것은 한 기본 형식에서 다른 기본 형식으로 변환하는 데 권장되는 언어 중립적 방법입니다. 또한 Convert.ChangeType(Object, Type, IFormatProvider) 메서드를 사용하여 지정된 사용자 지정 형식에서 다른 형식으로 변환할 수 있습니다.

기본 형식 간 변환

Convert 클래스는 기본 형식 간의 변환을 수행하는 언어 중립적 방법을 제공하며 공용 언어 런타임을 대상으로 하는 모든 언어에서 사용할 수 있습니다. 확장 및 축소 변환을 모두 위한 전체 메서드 세트를 제공하며, 지원되지 않는 변환(예: InvalidCastException 값을 정수 값으로 변환)에 대해서는 DateTime 예외를 발생시킵니다. 축소 변환은 확인된 컨텍스트에서 수행되며 변환에 실패하면 OverflowException throw됩니다.

중요합니다

Convert 클래스에는 각 기본 형식으로 변환하는 메서드가 포함되어 있으므로 각 기본 형식의 IConvertible 명시적 인터페이스 구현을 호출할 필요가 없습니다.

다음 예제에서는 System.Convert 클래스를 사용하여 .NET 기본 형식 간에 몇 가지 확대 및 축소 변환을 수행하는 방법을 보여 줍니다.

// Convert an Int32 value to a Decimal (a widening conversion).
int integralValue = 12534;
decimal decimalValue = Convert.ToDecimal(integralValue);
Console.WriteLine($"Converted the {integralValue.GetType().Name} value {integralValue} to " +
                                  "the {decimalValue.GetType().Name} value {decimalValue:N2}.");
// Convert a Byte value to an Int32 value (a widening conversion).
byte byteValue = Byte.MaxValue;
int integralValue2 = Convert.ToInt32(byteValue);
Console.WriteLine($"Converted the {byteValue.GetType().Name} value {byteValue} to " +
                                  "the {integralValue2.GetType().Name} value {integralValue2:G}.");

// Convert a Double value to an Int32 value (a narrowing conversion).
double doubleValue = 16.32513e12;
try
{
    long longValue = Convert.ToInt64(doubleValue);
    Console.WriteLine($"Converted the {doubleValue.GetType().Name} value {doubleValue:E} to " +
                                      "the {longValue.GetType().Name} value {longValue:N0}.");
}
catch (OverflowException)
{
    Console.WriteLine($"Unable to convert the {doubleValue.GetType().Name:E} value {doubleValue}.");
}

// Convert a signed byte to a byte (a narrowing conversion).
sbyte sbyteValue = -16;
try
{
    byte byteValue2 = Convert.ToByte(sbyteValue);
    Console.WriteLine($"Converted the {sbyteValue.GetType().Name} value {sbyteValue} to " +
                                      "the {byteValue2.GetType().Name} value {byteValue2:G}.");
}
catch (OverflowException)
{
    Console.WriteLine($"Unable to convert the {sbyteValue.GetType().Name} value {sbyteValue}.");
}
// The example displays the following output:
//       Converted the Int32 value 12534 to the Decimal value 12,534.00.
//       Converted the Byte value 255 to the Int32 value 255.
//       Converted the Double value 1.632513E+013 to the Int64 value 16,325,130,000,000.
//       Unable to convert the SByte value -16.
' Convert an Int32 value to a Decimal (a widening conversion).
Dim integralValue As Integer = 12534
Dim decimalValue As Decimal = Convert.ToDecimal(integralValue)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3:N2}.",
                  integralValue.GetType().Name,
                  integralValue,
                  decimalValue.GetType().Name,
                  decimalValue)

' Convert a Byte value to an Int32 value (a widening conversion).
Dim byteValue As Byte = Byte.MaxValue
Dim integralValue2 As Integer = Convert.ToInt32(byteValue)
Console.WriteLine("Converted the {0} value {1} to " +
                                  "the {2} value {3:G}.",
                                  byteValue.GetType().Name,
                                  byteValue,
                                  integralValue2.GetType().Name,
                                  integralValue2)

' Convert a Double value to an Int32 value (a narrowing conversion).
Dim doubleValue As Double = 16.32513e12
Try
    Dim longValue As Long = Convert.ToInt64(doubleValue)
    Console.WriteLine("Converted the {0} value {1:E} to " +
                                      "the {2} value {3:N0}.",
                                      doubleValue.GetType().Name,
                                      doubleValue,
                                      longValue.GetType().Name,
                                      longValue)
Catch e As OverflowException
    Console.WriteLine("Unable to convert the {0:E} value {1}.",
                                      doubleValue.GetType().Name, doubleValue)
End Try

' Convert a signed byte to a byte (a narrowing conversion).     
Dim sbyteValue As SByte = -16
Try
    Dim byteValue2 As Byte = Convert.ToByte(sbyteValue)
    Console.WriteLine("Converted the {0} value {1} to " +
                                      "the {2} value {3:G}.",
                                      sbyteValue.GetType().Name,
                                      sbyteValue,
                                      byteValue2.GetType().Name,
                                      byteValue2)
Catch e As OverflowException
    Console.WriteLine("Unable to convert the {0} value {1}.",
                                      sbyteValue.GetType().Name, sbyteValue)
End Try
' The example displays the following output:
'       Converted the Int32 value 12534 to the Decimal value 12,534.00.
'       Converted the Byte value 255 to the Int32 value 255.
'       Converted the Double value 1.632513E+013 to the Int64 value 16,325,130,000,000.
'       Unable to convert the SByte value -16.

경우에 따라, 특히 부동 소수점 값으로의 변환과 변환할 때 변환은 OverflowException을 throw하지 않더라도 정밀도 손실이 발생할 수 있습니다. 다음 예제에서는 이러한 정밀도 손실을 보여 줍니다. 첫 번째 경우 Decimal 값은 Double변환할 때 정밀도(유효 자릿수가 적음)가 적습니다. 두 번째 경우 변환을 완료하기 위해 Double 값이 42.72에서 43으로 반올림됩니다.

double doubleValue;

// Convert a Double to a Decimal.
decimal decimalValue = 13956810.96702888123451471211m;
doubleValue = Convert.ToDouble(decimalValue);
Console.WriteLine($"{decimalValue} converted to {doubleValue}.");

doubleValue = 42.72;
try
{
    int integerValue = Convert.ToInt32(doubleValue);
    Console.WriteLine($"{doubleValue} converted to {integerValue}.");
}
catch (OverflowException)
{
    Console.WriteLine($"Unable to convert {doubleValue} to an integer.");
}
// The example displays the following output:
//       13956810.96702888123451471211 converted to 13956810.9670289.
//       42.72 converted to 43.
Dim doubleValue As Double

' Convert a Double to a Decimal.
Dim decimalValue As Decimal = 13956810.96702888123451471211d
doubleValue = Convert.ToDouble(decimalValue)
Console.WriteLine("{0} converted to {1}.", decimalValue, doubleValue)

doubleValue = 42.72
Try
    Dim integerValue As Integer = Convert.ToInt32(doubleValue)
    Console.WriteLine("{0} converted to {1}.",
                                      doubleValue, integerValue)
Catch e As OverflowException
    Console.WriteLine("Unable to convert {0} to an integer.",
                                      doubleValue)
End Try
' The example displays the following output:
'       13956810.96702888123451471211 converted to 13956810.9670289.
'       42.72 converted to 43.

Convert 클래스에서 지원하는 확대 및 축소 변환을 모두 나열하는 테이블은 형식 변환 테이블참조하세요.

ChangeType 메서드를 사용하여 사용자 지정 변환

각 기본 형식으로의 변환을 지원하는 것 외에도 Convert 클래스를 사용하여 사용자 지정 형식을 하나 이상의 미리 정의된 형식으로 변환할 수 있습니다. 이 변환은 Convert.ChangeType(Object, Type, IFormatProvider) 메서드에 의해 수행되며, IConvertible.ToType 매개 변수의 value 메서드에 대한 호출을 래핑합니다. 즉, value 매개 변수로 표시되는 개체는 IConvertible 인터페이스의 구현을 제공해야 합니다.

비고

Convert.ChangeType(Object, Type)Convert.ChangeType(Object, Type, IFormatProvider) 메서드는 Type 개체를 사용하여 value 변환되는 대상 형식을 지정하기 때문에 컴파일 시간에 형식을 알 수 없는 개체로 동적 변환을 수행하는 데 사용할 수 있습니다. 그러나 IConvertiblevalue 구현은 여전히 이 변환을 지원해야 합니다.

다음 예제에서는 IConvertible 개체를 TemperatureCelsius 개체로 변환할 수 있도록 하는 TemperatureFahrenheit 인터페이스의 가능한 구현을 보여 줍니다. 이 예제에서는 Temperature 인터페이스를 구현하고 IConvertible 메서드를 재정의하는 기본 클래스 Object.ToString정의합니다. 파생된 TemperatureCelsiusTemperatureFahrenheit 클래스는 각각 기본 클래스의 ToTypeToString 메서드를 재정의합니다.

using System;

public abstract class Temperature : IConvertible
{
    protected decimal temp;

    public Temperature(decimal temperature)
    {
        this.temp = temperature;
    }

    public decimal Value
    {
        get { return this.temp; }
        set { this.temp = value; }
    }

    public override string ToString()
    {
        return temp.ToString(null as IFormatProvider) + "º";
    }

    // IConvertible implementations.
    public TypeCode GetTypeCode()
    {
        return TypeCode.Object;
    }

    public bool ToBoolean(IFormatProvider provider)
    {
        throw new InvalidCastException(String.Format("Temperature-to-Boolean conversion is not supported."));
    }

    public byte ToByte(IFormatProvider provider)
    {
        if (temp < Byte.MinValue || temp > Byte.MaxValue)
            throw new OverflowException(String.Format("{0} is out of range of the Byte data type.", temp));
        else
            return (byte)temp;
    }

    public char ToChar(IFormatProvider provider)
    {
        throw new InvalidCastException("Temperature-to-Char conversion is not supported.");
    }

    public DateTime ToDateTime(IFormatProvider provider)
    {
        throw new InvalidCastException("Temperature-to-DateTime conversion is not supported.");
    }

    public decimal ToDecimal(IFormatProvider provider)
    {
        return temp;
    }

    public double ToDouble(IFormatProvider provider)
    {
        return (double)temp;
    }

    public short ToInt16(IFormatProvider provider)
    {
        if (temp < Int16.MinValue || temp > Int16.MaxValue)
            throw new OverflowException(String.Format("{0} is out of range of the Int16 data type.", temp));
        else
            return (short)Math.Round(temp);
    }

    public int ToInt32(IFormatProvider provider)
    {
        if (temp < Int32.MinValue || temp > Int32.MaxValue)
            throw new OverflowException(String.Format("{0} is out of range of the Int32 data type.", temp));
        else
            return (int)Math.Round(temp);
    }

    public long ToInt64(IFormatProvider provider)
    {
        if (temp < Int64.MinValue || temp > Int64.MaxValue)
            throw new OverflowException(String.Format("{0} is out of range of the Int64 data type.", temp));
        else
            return (long)Math.Round(temp);
    }

    public sbyte ToSByte(IFormatProvider provider)
    {
        if (temp < SByte.MinValue || temp > SByte.MaxValue)
            throw new OverflowException(String.Format("{0} is out of range of the SByte data type.", temp));
        else
            return (sbyte)temp;
    }

    public float ToSingle(IFormatProvider provider)
    {
        return (float)temp;
    }

    public virtual string ToString(IFormatProvider provider)
    {
        return temp.ToString(provider) + "°";
    }

    // If conversionType is implemented by another IConvertible method, call it.
    public virtual object ToType(Type conversionType, IFormatProvider provider)
    {
        switch (Type.GetTypeCode(conversionType))
        {
            case TypeCode.Boolean:
                return this.ToBoolean(provider);
            case TypeCode.Byte:
                return this.ToByte(provider);
            case TypeCode.Char:
                return this.ToChar(provider);
            case TypeCode.DateTime:
                return this.ToDateTime(provider);
            case TypeCode.Decimal:
                return this.ToDecimal(provider);
            case TypeCode.Double:
                return this.ToDouble(provider);
            case TypeCode.Empty:
                throw new NullReferenceException("The target type is null.");
            case TypeCode.Int16:
                return this.ToInt16(provider);
            case TypeCode.Int32:
                return this.ToInt32(provider);
            case TypeCode.Int64:
                return this.ToInt64(provider);
            case TypeCode.Object:
                // Leave conversion of non-base types to derived classes.
                throw new InvalidCastException(String.Format("Cannot convert from Temperature to {0}.",
                                               conversionType.Name));
            case TypeCode.SByte:
                return this.ToSByte(provider);
            case TypeCode.Single:
                return this.ToSingle(provider);
            case TypeCode.String:
                IConvertible iconv = this;
                return iconv.ToString(provider);
            case TypeCode.UInt16:
                return this.ToUInt16(provider);
            case TypeCode.UInt32:
                return this.ToUInt32(provider);
            case TypeCode.UInt64:
                return this.ToUInt64(provider);
            default:
                throw new InvalidCastException("Conversion not supported.");
        }
    }

    public ushort ToUInt16(IFormatProvider provider)
    {
        if (temp < UInt16.MinValue || temp > UInt16.MaxValue)
            throw new OverflowException(String.Format("{0} is out of range of the UInt16 data type.", temp));
        else
            return (ushort)Math.Round(temp);
    }

    public uint ToUInt32(IFormatProvider provider)
    {
        if (temp < UInt32.MinValue || temp > UInt32.MaxValue)
            throw new OverflowException(String.Format("{0} is out of range of the UInt32 data type.", temp));
        else
            return (uint)Math.Round(temp);
    }

    public ulong ToUInt64(IFormatProvider provider)
    {
        if (temp < UInt64.MinValue || temp > UInt64.MaxValue)
            throw new OverflowException(String.Format("{0} is out of range of the UInt64 data type.", temp));
        else
            return (ulong)Math.Round(temp);
    }
}

public class TemperatureCelsius : Temperature, IConvertible
{
    public TemperatureCelsius(decimal value) : base(value)
    {
    }

    // Override ToString methods.
    public override string ToString()
    {
        return this.ToString(null);
    }

    public override string ToString(IFormatProvider provider)
    {
        return temp.ToString(provider) + "°C";
    }

    // If conversionType is a implemented by another IConvertible method, call it.
    public override object ToType(Type conversionType, IFormatProvider provider)
    {
        // For non-objects, call base method.
        if (Type.GetTypeCode(conversionType) != TypeCode.Object)
        {
            return base.ToType(conversionType, provider);
        }
        else
        {
            if (conversionType.Equals(typeof(TemperatureCelsius)))
                return this;
            else if (conversionType.Equals(typeof(TemperatureFahrenheit)))
                return new TemperatureFahrenheit((decimal)this.temp * 9 / 5 + 32);
            else
                throw new InvalidCastException(String.Format("Cannot convert from Temperature to {0}.",
                                               conversionType.Name));
        }
    }
}

public class TemperatureFahrenheit : Temperature, IConvertible
{
    public TemperatureFahrenheit(decimal value) : base(value)
    {
    }

    // Override ToString methods.
    public override string ToString()
    {
        return this.ToString(null);
    }

    public override string ToString(IFormatProvider provider)
    {
        return temp.ToString(provider) + "°F";
    }

    public override object ToType(Type conversionType, IFormatProvider provider)
    {
        // For non-objects, call base method.
        if (Type.GetTypeCode(conversionType) != TypeCode.Object)
        {
            return base.ToType(conversionType, provider);
        }
        else
        {
            // Handle conversion between derived classes.
            if (conversionType.Equals(typeof(TemperatureFahrenheit)))
                return this;
            else if (conversionType.Equals(typeof(TemperatureCelsius)))
                return new TemperatureCelsius((decimal)(this.temp - 32) * 5 / 9);
            // Unspecified object type: throw an InvalidCastException.
            else
                throw new InvalidCastException(String.Format("Cannot convert from Temperature to {0}.",
                                               conversionType.Name));
        }
    }
}
Public MustInherit Class Temperature
    Implements IConvertible

    Protected temp As Decimal

    Public Sub New(temperature As Decimal)
        Me.temp = temperature
    End Sub

    Public Property Value As Decimal
        Get
            Return Me.temp
        End Get
        Set
            Me.temp = Value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return temp.ToString() & "º"
    End Function

    ' IConvertible implementations.
    Public Function GetTypeCode() As TypeCode Implements IConvertible.GetTypeCode
        Return TypeCode.Object
    End Function

    Public Function ToBoolean(provider As IFormatProvider) As Boolean Implements IConvertible.ToBoolean
        Throw New InvalidCastException(String.Format("Temperature-to-Boolean conversion is not supported."))
    End Function

    Public Function ToByte(provider As IFormatProvider) As Byte Implements IConvertible.ToByte
        If temp < Byte.MinValue Or temp > Byte.MaxValue Then
            Throw New OverflowException(String.Format("{0} is out of range of the Byte data type.", temp))
        Else
            Return CByte(temp)
        End If
    End Function

    Public Function ToChar(provider As IFormatProvider) As Char Implements IConvertible.ToChar
        Throw New InvalidCastException("Temperature-to-Char conversion is not supported.")
    End Function

    Public Function ToDateTime(provider As IFormatProvider) As DateTime Implements IConvertible.ToDateTime
        Throw New InvalidCastException("Temperature-to-DateTime conversion is not supported.")
    End Function

    Public Function ToDecimal(provider As IFormatProvider) As Decimal Implements IConvertible.ToDecimal
        Return temp
    End Function

    Public Function ToDouble(provider As IFormatProvider) As Double Implements IConvertible.ToDouble
        Return CDbl(temp)
    End Function

    Public Function ToInt16(provider As IFormatProvider) As Int16 Implements IConvertible.ToInt16
        If temp < Int16.MinValue Or temp > Int16.MaxValue Then
            Throw New OverflowException(String.Format("{0} is out of range of the Int16 data type.", temp))
        End If
        Return CShort(Math.Round(temp))
    End Function

    Public Function ToInt32(provider As IFormatProvider) As Int32 Implements IConvertible.ToInt32
        If temp < Int32.MinValue Or temp > Int32.MaxValue Then
            Throw New OverflowException(String.Format("{0} is out of range of the Int32 data type.", temp))
        End If
        Return CInt(Math.Round(temp))
    End Function

    Public Function ToInt64(provider As IFormatProvider) As Int64 Implements IConvertible.ToInt64
        If temp < Int64.MinValue Or temp > Int64.MaxValue Then
            Throw New OverflowException(String.Format("{0} is out of range of the Int64 data type.", temp))
        End If
        Return CLng(Math.Round(temp))
    End Function

    Public Function ToSByte(provider As IFormatProvider) As SByte Implements IConvertible.ToSByte
        If temp < SByte.MinValue Or temp > SByte.MaxValue Then
            Throw New OverflowException(String.Format("{0} is out of range of the SByte data type.", temp))
        Else
            Return CSByte(temp)
        End If
    End Function

    Public Function ToSingle(provider As IFormatProvider) As Single Implements IConvertible.ToSingle
        Return CSng(temp)
    End Function

    Public Overridable Overloads Function ToString(provider As IFormatProvider) As String Implements IConvertible.ToString
        Return temp.ToString(provider) & " °C"
    End Function

    ' If conversionType is a implemented by another IConvertible method, call it.
    Public Overridable Function ToType(conversionType As Type, provider As IFormatProvider) As Object Implements IConvertible.ToType
        Select Case Type.GetTypeCode(conversionType)
            Case TypeCode.Boolean
                Return Me.ToBoolean(provider)
            Case TypeCode.Byte
                Return Me.ToByte(provider)
            Case TypeCode.Char
                Return Me.ToChar(provider)
            Case TypeCode.DateTime
                Return Me.ToDateTime(provider)
            Case TypeCode.Decimal
                Return Me.ToDecimal(provider)
            Case TypeCode.Double
                Return Me.ToDouble(provider)
            Case TypeCode.Empty
                Throw New NullReferenceException("The target type is null.")
            Case TypeCode.Int16
                Return Me.ToInt16(provider)
            Case TypeCode.Int32
                Return Me.ToInt32(provider)
            Case TypeCode.Int64
                Return Me.ToInt64(provider)
            Case TypeCode.Object
                ' Leave conversion of non-base types to derived classes.
                Throw New InvalidCastException(String.Format("Cannot convert from Temperature to {0}.", _
                                               conversionType.Name))
            Case TypeCode.SByte
                Return Me.ToSByte(provider)
            Case TypeCode.Single
                Return Me.ToSingle(provider)
            Case TypeCode.String
                Return Me.ToString(provider)
            Case TypeCode.UInt16
                Return Me.ToUInt16(provider)
            Case TypeCode.UInt32
                Return Me.ToUInt32(provider)
            Case TypeCode.UInt64
                Return Me.ToUInt64(provider)
            Case Else
                Throw New InvalidCastException("Conversion not supported.")
        End Select
    End Function

    Public Function ToUInt16(provider As IFormatProvider) As UInt16 Implements IConvertible.ToUInt16
        If temp < UInt16.MinValue Or temp > UInt16.MaxValue Then
            Throw New OverflowException(String.Format("{0} is out of range of the UInt16 data type.", temp))
        End If
        Return CUShort(Math.Round(temp))
    End Function

    Public Function ToUInt32(provider As IFormatProvider) As UInt32 Implements IConvertible.ToUInt32
        If temp < UInt32.MinValue Or temp > UInt32.MaxValue Then
            Throw New OverflowException(String.Format("{0} is out of range of the UInt32 data type.", temp))
        End If
        Return CUInt(Math.Round(temp))
    End Function

    Public Function ToUInt64(provider As IFormatProvider) As UInt64 Implements IConvertible.ToUInt64
        If temp < UInt64.MinValue Or temp > UInt64.MaxValue Then
            Throw New OverflowException(String.Format("{0} is out of range of the UInt64 data type.", temp))
        End If
        Return CULng(Math.Round(temp))
    End Function
End Class

Public Class TemperatureCelsius : Inherits Temperature : Implements IConvertible
    Public Sub New(value As Decimal)
        MyBase.New(value)
    End Sub

    ' Override ToString methods.
    Public Overrides Function ToString() As String
        Return Me.ToString(Nothing)
    End Function

    Public Overrides Function ToString(provider As IFormatProvider) As String
        Return temp.ToString(provider) + "°C"
    End Function

    ' If conversionType is a implemented by another IConvertible method, call it.
    Public Overrides Function ToType(conversionType As Type, provider As IFormatProvider) As Object
        ' For non-objects, call base method.
        If Type.GetTypeCode(conversionType) <> TypeCode.Object Then
            Return MyBase.ToType(conversionType, provider)
        Else
            If conversionType.Equals(GetType(TemperatureCelsius)) Then
                Return Me
            ElseIf conversionType.Equals(GetType(TemperatureFahrenheit))
                Return New TemperatureFahrenheit(CDec(Me.temp * 9 / 5 + 32))
                ' Unspecified object type: throw an InvalidCastException.
            Else
                Throw New InvalidCastException(String.Format("Cannot convert from Temperature to {0}.", _
                                               conversionType.Name))
            End If
        End If
    End Function
End Class

Public Class TemperatureFahrenheit : Inherits Temperature : Implements IConvertible
    Public Sub New(value As Decimal)
        MyBase.New(value)
    End Sub

    ' Override ToString methods.
    Public Overrides Function ToString() As String
        Return Me.ToString(Nothing)
    End Function

    Public Overrides Function ToString(provider As IFormatProvider) As String
        Return temp.ToString(provider) + "°F"
    End Function

    Public Overrides Function ToType(conversionType As Type, provider As IFormatProvider) As Object
        ' For non-objects, call base method.
        If Type.GetTypeCode(conversionType) <> TypeCode.Object Then
            Return MyBase.ToType(conversionType, provider)
        Else
            ' Handle conversion between derived classes.
            If conversionType.Equals(GetType(TemperatureFahrenheit)) Then
                Return Me
            ElseIf conversionType.Equals(GetType(TemperatureCelsius))
                Return New TemperatureCelsius(CDec((MyBase.temp - 32) * 5 / 9))
                ' Unspecified object type: throw an InvalidCastException.
            Else
                Throw New InvalidCastException(String.Format("Cannot convert from Temperature to {0}.", _
                                               conversionType.Name))
            End If
        End If
    End Function
End Class

다음 예제에서는 IConvertible 개체를 TemperatureCelsius 개체로 변환하는 이러한 TemperatureFahrenheit 구현에 대한 몇 가지 호출을 보여 줍니다.

TemperatureCelsius tempC1 = new TemperatureCelsius(0);
TemperatureFahrenheit tempF1 = (TemperatureFahrenheit)Convert.ChangeType(tempC1, typeof(TemperatureFahrenheit), null);
Console.WriteLine($"{tempC1} equals {tempF1}.");
TemperatureCelsius tempC2 = (TemperatureCelsius)Convert.ChangeType(tempC1, typeof(TemperatureCelsius), null);
Console.WriteLine($"{tempC1} equals {tempC2}.");
TemperatureFahrenheit tempF2 = new TemperatureFahrenheit(212);
TemperatureCelsius tempC3 = (TemperatureCelsius)Convert.ChangeType(tempF2, typeof(TemperatureCelsius), null);
Console.WriteLine($"{tempF2} equals {tempC3}.");
TemperatureFahrenheit tempF3 = (TemperatureFahrenheit)Convert.ChangeType(tempF2, typeof(TemperatureFahrenheit), null);
Console.WriteLine($"{tempF2} equals {tempF3}.");
// The example displays the following output:
//       0°C equals 32°F.
//       0°C equals 0°C.
//       212°F equals 100°C.
//       212°F equals 212°F.
Dim tempC1 As New TemperatureCelsius(0)
Dim tempF1 As TemperatureFahrenheit = CType(Convert.ChangeType(tempC1, GetType(TemperatureFahrenheit), Nothing), TemperatureFahrenheit)
Console.WriteLine("{0} equals {1}.", tempC1, tempF1)
Dim tempC2 As TemperatureCelsius = CType(Convert.ChangeType(tempC1, GetType(TemperatureCelsius), Nothing), TemperatureCelsius)
Console.WriteLine("{0} equals {1}.", tempC1, tempC2)
Dim tempF2 As New TemperatureFahrenheit(212)
Dim tempC3 As TEmperatureCelsius = CType(Convert.ChangeType(tempF2, GEtType(TemperatureCelsius), Nothing), TemperatureCelsius)
Console.WriteLine("{0} equals {1}.", tempF2, tempC3)
Dim tempF3 As TemperatureFahrenheit = CType(Convert.ChangeType(tempF2, GetType(TemperatureFahrenheit), Nothing), TemperatureFahrenheit)
Console.WriteLine("{0} equals {1}.", tempF2, tempF3)
' The example displays the following output:
'       0°C equals 32°F.
'       0°C equals 0°C.
'       212°F equals 100°C.
'       212°F equals 212°F.

TypeConverter 클래스

또한 .NET을 사용하면 System.ComponentModel.TypeConverter 클래스를 확장하고 System.ComponentModel.TypeConverterAttribute 특성을 통해 형식 변환기를 형식과 연결하여 사용자 지정 형식에 대한 형식 변환기를 정의할 수 있습니다. 다음 표에서는 이 접근 방식과 사용자 지정 형식에 대한 IConvertible 인터페이스 구현 간의 차이점을 강조 표시합니다.

비고

사용자 지정 형식에 대해 정의된 형식 변환기가 있는 경우에만 디자인 타임 지원을 제공할 수 있습니다.

TypeConverter를 사용한 변환 IConvertible을 사용한 변환
사용자 지정 형식에 대해 TypeConverter에서 별도의 클래스를 파생시켜 구현합니다. 이 파생 클래스는 TypeConverterAttribute 특성을 적용하여 사용자 지정 형식과 연결됩니다. 변환을 수행하기 위해 사용자 지정 형식에 의해 구현됩니다. 형식의 사용자는 형식에 대한 IConvertible 변환 메서드를 호출합니다.
디자인 타임과 런타임에 모두 사용할 수 있습니다. 런타임에만 사용할 수 있습니다.
리플렉션을 사용하기 때문에 IConvertible로 활성화된 변환보다 느립니다. 리플렉션을 사용하지 않습니다.
사용자 지정 형식에서 다른 데이터 형식으로, 다른 데이터 형식에서 사용자 지정 형식으로 양방향 형식 변환을 허용합니다. 예를 들어, TypeConverter에 대해 정의된 MyTypeMyType에서 String으로의 변환과 String에서 MyType으로의 변환을 허용합니다. 사용자 지정 형식에서 다른 데이터 형식으로 변환할 수 있지만 다른 데이터 형식에서 사용자 지정 형식으로 변환할 수는 없습니다.

형식 변환기를 사용하여 변환을 수행하는 방법에 대한 자세한 내용은 System.ComponentModel.TypeConverter참조하세요.

참고하십시오