经过前面的学习c#中基本的数据类型就介绍的差不多了,下面就学习下类型之间的互相转换.c# 中类型转换可以分为2类: 隐式转换 和 显式转换.
隐式转换:
隐式转换是系统默认的转换,不需要申明就可以进行转换。在隐式转换过程中,编译器无需对转换进行检查就能够安全的执行转换,比如从int类型转到long类型,就是隐式转换。隐式转换一般不会失败,转换过程中也不会丢失信息.
比如:int i = 100;
long a = i; //无需声明自动的将int 型转换为long型
隐式转换并非对任意两种类型都成立,比如,我们将上面的long类型隐式转换为int类型就不会成功:
long a = 100;
int i = a; //编译器会报错
因此隐式转换有以下的规则:
隐式数值转换
隐式枚举转换
隐式引用转换
隐式数值转换:
隐式数值转换包括以下几种:
从sbyte 类型到short、int、long、float、double、decimal类型;
从byte 类型到short、ushort、int、uint、long、ulong、float、double、decimal类型;
从short 类型到 int、long、flaot、double、decimal类型;
从ushort 类型到 int、uint、long、ulong、flaot、double、decimal类型;
从int 类型到 long、flaot、double、decimal类型;
从uint 类型到 long、ulong、flaot、double、decimal类型;
从long 类型到 float、double、decimal类型;
从ulong 类型到 float、double、decimal类型;
从char 类型到 ushort、int、uint、long、ulong、flaot、double、decimal类型;
从float 类型到 double类型;
写了这么多总结下吧,概括的说就是从低精度类型到高精度类型转换(因为不丢失精度与数据信息),而从高精度类型到低精度不能隐式转换(可能会丢失部分信息,不安全)。有关类型的精度与范围请参照 c#学习日记04 。这里要提醒的是 不存在其他类型到char类型的隐式转换。
隐式数值转换实例:
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace test
{
class program
{
static void main(string[] args)
{
byte x = 255; //byte 表示的范围0~255
short y = x; //将从byte到short隐式转换
y++;
console.writeline("y = {0}",y);
y = 32767; //shot的范围 -32768~32767
int i = y + 5; //从 short 到 int 隐式转换扩大范围 结果是准确的
y+=5; //超出范围了结果会不准确
console.writeline("y = {0}",y); //y超出范围数据会丢失部分
console.writeline("i = {0}",i);
}
}
}
结果:
从这个例子可以看出,及时的采用类型转换还是很重要哒。
隐式枚举转换:
隐式枚举转换允许把十进制0,转换为任何枚举类型,注意的是,它只能转换0,对其他整数不存在这种隐式转换,看下面的例子:
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace test
{
class program
{
enum weekday //定义一个枚举类型
{ sunday, monday, tuesday, wednesday, thursday, friday, saturday };
static void main(string[] args)
{
weekday day;
day = 0; //隐式将0转换为枚举类型(只能是0)
console.writeline(day);
}
}
}
输出结果是:
sunday
上面代码中如果我们把 day = 0 该为 day = 1 编译器就会给出错误。
隐式引用转换:
从任何引用类型到对象类型的转换;
(person p = new person())
从类类型s到类类型t的转换,其中s是t的派生类;
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace test
{
class person //定义了一个基类(父类) person
{
}
class person1 : person // person1 派生于基类person,person1就叫person的一个子类,
{
}
class program
{
static void main(string[] args)
{
person1 per = new person1(); //将子类person1实例化一个对象per
person per = per; //将子类隐式转换为父类
}
}
}
从类类型s到接口类型t的转换,其中类s实现了接口t;(有关接口(interface)的内容后面会写到,用它只声明方法不定义方法)
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace test
{
public interface infa //定义了一个接口
{
void output();
}
class person : infa //定义一个person类继承于接口并实现方法
{
public void output()
{
console.writeline("welcome");
}
}
class program
{
static void main(string[] args)
{
person per = new person(); //实例化
infa fa = per; //从person到interface(接口)隐式转换
}
}
}
从接口类型s到接口类型t的转换,其中t是s的父接口;
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace test
{
public interface infa //定义了一个接口
{
void output(); //接口只声明方法,具体实现由它的派生类写代码决定
}
public interface infa1 : infa //定义一个infa1接口继承于infa接口
{
void input();
}
class person1 : infa1 //由infa1派生一个person1类,因为接口不能直接实例化
{
}
class program
{
static void main(string[] args)
{
person1 per = new person1 { }; //接口不能直接实例化,需要实例化一个派生于infa1接口person1类
infa fa = per; //实现子接口到父借口隐式转换
}
}
}
引用类型数组s到引用类型数组t转换,其中s是t的派生类,并且数组维数得相同;
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace test
{
class person //定义一个基类 person
{
}
class person1 : person //由基类派生一个子类person1
{
}
class program
{
static void main(string[] args)
{
person1[] per = new person1[5]; //实例化一个person1
person[] per = per; //实现隐式转换
}
}
}
在这里要提醒的是,引用类型数组 如果是值类型数组以下代码就会报错:
class program
{
static void main(string[] args)
{
int[] n_int = new int[10];
double[] n_doubel = new double[10];
n_doubel = n_int; //这里报错啦
}
}
从数组类型到system.array的转换;(array是所有数组的基类 参考上一篇^_^)
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace test
{
class program
{
static void main(string[] args)
{
int[] n_int = new int[10]; //实例化一个int类型的数组 n_int
array arr = n_int; // array表示的就是数组 所以不能array[] arr
}
}
从任何代表类型到system.delegate的转换;(后面会写到delegate)
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace test
{
class program
{
public static int output(int s) //定义一个方法
{
console.writeline("welcome,{0}",s);
return 1;
}
public delegate int mydel(int s); //声明一个委托(以后我会说到委托)
static void main(string[] args)
{
mydel my = new mydel(output); //将 output方法委托给my
delegate mydel = my; //向 mydel 隐式转换
}
}
}
以上就是 c#学习日记16----隐式转换具体用例的内容。