隐式转换:
隐式转换是体系默许的转换,不须要说明就可以举行转换。在隐式转换过程当中,编译器无需对转换举行检查就可以平安的实行转换,比方从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----隐式转换详细用例的内容,更多相关内容请关注ki4网(www.ki4.cn)!