一、分类
类型转换分为显示类型转换和隐式类型转换
例如如下例子:
int i=3;
long l=i; //隐式类型转换
short s =(short)I; //显示类型转换
转换过程中可能会导致数据的丢失,例如long类型存储的数据比较大,放到short中可能会有数据丢失。
二、用户自定义强制类型转换
如下代码所示,如果我们想要将结构体成员转换为float怎么计算。.NET本身并没有将结构转换为flaot的方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleCurrency
{
struct Currency
{
public uint Dollars; //两个成员变量
public ushort Cents;
public Currency(uint dollars, ushort cents) //有参构造函数,用作初始化用
{
this.Dollars = dollars;
this.Cents = cents;
}
public override string ToString() //重写ToString方法
{
return string