C#学习日记16----隐式转换具体用例
来源:互联网 发布:java中数组去重 编辑:程序博客网 时间:2024/05/16 23:52
经过前面的学习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 隐式转换 } }}
常用到的就介绍这么多了,需要补充请私信我,有关委托我后面会写到的,请继续支持HC66,感谢你的阅读^_^,
- C#学习日记16----隐式转换具体用例
- C#学习日记17---显示类型转换具体用例
- UML建模《用例图》----具体用例和抽象用例
- Activity实现OnclickListener接口的思想和具体用例
- 设计一个计算体重的APP(具体用页面带值转换)(详细)
- Activity实现OnclickListener接口的思想和具体用例(转载)
- C#学习日记10----番外篇 C语言进制转换
- c# 工作学习日记
- C#学习日记
- C#学习日记1
- C#学习日记 链表
- C#学习日记 StringBuilder
- C#学习日记 字典
- C#学习日记 指针
- C#学习日记18----装箱(boxing)转换与拆箱(unboxing)转换
- C# 多线程教程(C#学习日记 2017-10-16)
- MyISAM和InnoDB具体用在什么情况下?
- 黑马程序员--.Net学习日记——C#中的类型转换
- opencv3.0, vs2013配置
- 给LinearLayout设置点击事件
- C#学习之多线程开发技术(二)
- Android中ListView包含CheckBox时滑动丢失选中状态的解决
- struts2笔记-国际化
- C#学习日记16----隐式转换具体用例
- 点击return和空白回收键盘跳到下一个
- Cocos 3D功能初探学习笔记(3)---光照
- UI 动画之UIView动画 实现两个页面之间的切换
- Linux Shell 之 Shell 基本控制结构(一)(if and case)
- 在Android中操作JSON数据
- Big Event in HDU 背包
- 英语语法 限定词
- hdu4501(三维背包)