System.Convert.ChangeType 转换数据类型

返回具有指定 Type 而且其值等效于指定对象的 Object

参数
value
类型: System..::.Object

实现 IConvertible 接口的 Object

conversionType
类型: System..::.Type

Type

返回值

类型:System..::.Object

一个对象,它的 TypeconversionType,而且它的值等效于 value

- 或 -

如果 valuenullNothingnullptrnull 引用(在 Visual Basic 中为 NothingconversionType 不是值类型,则为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing

异常条件
InvalidCastException

不支持此转换。

- 或 -

valuenullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing,而且 conversionType 是值类型。

ArgumentNullException

conversionTypenullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing

此方法将当前线程的区域性用于转换。

 

using System;

public class ChangeTypeTest {
    public static void Main() {

        Double d = -2.345;
        int i = (int)Convert.ChangeType(d, typeof(int));

        Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i);

        string s = "12/12/98";
        DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));

        Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt);       
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值