ChangeType() 메서드는 지정된 유형의 개체를 반환하고 그 값은 지정된 개체와 동일합니다.
이중 유형이 있다고 가정해 보겠습니다.
double val = -3.456
이제 ChangeType 메서드를 사용하여 유형을 정수로 변경합니다.
num = (int)Convert.ChangeType(val, TypeCode.Int32);
전체 예를 살펴보겠습니다.
예시
using System;
public class Demo {
public static void Main() {
double val = -3.456;
int num = (int)Convert.ChangeType(val, TypeCode.Int32);
Console.WriteLine("{0} converted to an Int32: {1}", val, num);
}
} 출력
-3.456 converted to an Int32: -3