显现范例转换,又叫强迫范例转换,在举行转换的时刻它须要我们明白的指定转换范例. 比方,当我们把long范例转换为int范例时,由于这类转换是丧失精度的转换,体系不会自动举行隐式转换,所以须要举行强迫转换:
long l = 6000; int i = (int)l; //须要用在 ()内里声明转换范例
显现范例转换并非是对恣意2种范例都建立,比方:
int i = 6000; string i = (string)i; //这里会报错
因而显现范例转换也是有肯定划定规矩的:
显现数值转换;
显现罗列转换;
显现援用转换;
显现转换并非总能胜利,而且经常能够引发信息的丧失(由于范例差别,局限、精度也是差别的 概况参照数据范例),显现转换包含一切的隐式转换,因而也能够把隐式转换写成显现转换的情势,比方:
int i = 6000; long l = (long)i; //等价于 long l = i;
显现数值转换:
显现数值转换,是指值范例与值范例之间的转换,有以下的划定规矩:
从 sbyte 到 byte、ushort、uint、ulong、char范例;
从 byte 到 sbyte、char范例;
从 short 到 sbyte、byte、ushort、uint、ulong、char范例;
从 ushort 到 sbyte、byte、short 、char范例;
从 int 到 sbyte、byte、short、ushort、uint、ulong、char范例;
从 uint 到 sbyte、byte、short、ushort、int、char范例;
从 long 到 sbyte、byte、short、ushort、int、uint、ulong、char范例;
从 ulong 到 sbyte、byte、short、ushort、int、uint、long、char范例;
从 char 到 sbyte、byte、short范例;
从 float 到 sbyte、byte、short、ushort、int、uint、long、ulong、char、decimal范例;
从 double 到 sbyte、byte、short、ushort、int、uint、long、ulong、float、char、decimal范例;
从 decimal 到 sbyte、byte、short、ushort、int、uint、long、ulong、float、char、double范例;
写了这么多总结下吧,就是从高精度到低精度的转换,有多是保存转换也有多是四舍五入转换,写个例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { static void Main(string[] args) { double n_double = 1.73456789; float n_float = (float)n_double; //显现转换 float的有用为只要8位(.也是一名)所以从第9位四舍五入 int n_int = (int)n_double; //只保存整数 Console.WriteLine("n_float = {0}\nn_int = {1}",n_float,n_int); } } }
运转效果:
对照发明当double 数据局限超越float的有用值局限,显现转换时对第9位四舍五入,转换为int范例时只保存整数部份。
显现罗列转换:
显现罗列转换包含下面几个内容:
从sbyte、byte、short、ushort、int、uint、long、ulong、float、char、double、decimal范例到任何 罗列 范例;
从任何罗列范例 到 sbyte、byte、short、ushort、int、uint、long、ulong、float、char、double、decimal范例;
从任何罗列范例 到 任何其他罗列范例;
写个列子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { enum weekday //定义2个罗列 {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday } enum Month {Janurary=1,February,March,April,May,Jun,July } static void Main(string[] args) { int n_int = 2; double n_double = 3.0; decimal n_decimal = 5m; //声明decimal 范例要加m weekday weki = (weekday)n_int; //从int、double、decimal到罗列转换 weekday wekd = (weekday)n_double; weekday wekde = (weekday)n_decimal; weekday wek = weekday.Tuesday; //罗列范例之间的转换 Month mon = (Month)wek; int i = (int)wek; //从罗列范例到int的转换 int t = (int)mon; Console.WriteLine("n_int = {0}\nn_double = {1}\nn_decimal = {2}",weki,wekd,wekde); Console.WriteLine("wek = {0}\nmon = {1}\nwek ={2}\tmon = {3}",wek,mon,i,t); } } }
运转效果:
显现援用转换:
从对象到任何援用范例的转换;
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { //定义2个类 teacher与man class teacher { } class man { } static void Main(string[] args) { man per = new man(); //将man实例化一个对象per object o = per; //装箱 teacher p = (teacher)o; // 将o显现转换为teacher类 } } }
从类范例s到类范例t的转换,个中s是t的基类;
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { class man //定义一个基类 { } class student:man //student继续man { } static void Main(string[] args) { man per = new man(); //man实例化一个对象per student stu = (student)per; //将父类转换为子类 } } }
从类范例s到接口t的转换,个中s不是密封类,并没有完成t;(有关接口(interface)的内容背面会写到,它只声明要领不定义要领)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test { class Program { public interface teacher //定义一个接口 { } class student //定义一个类 { } static void Main(string[] args) { student stu = new student(); //实例化一个对象 teacher tea = (teacher)stu; // 显现转换 } } }
从接口型s到类范例t的转换,个中t不是密封类,并没有完成s;
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { public interface man //定义一个接口 { } class teacher:man //定义一个继续于man的类man { } class student //定义一个新类 { } static void Main(string[] args) { man teac=new teacher(); //间接实例化一个接口 student stu = (student)teac; // 显现转换 } } }
从接口范例s到接口范例t的转换,个中s不是t的子接口;
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { public interface man //定义一个接口 { } class teacher : man //由接口派生一个类 { } public interface person //定义一个接口 { } class student:person //由接口派生一个类 { } static void Main(string[] args) { man teac=new teacher(); //间接实例化一个接口 person stu = (person)teac; // 显现转换 } } }
援用范例数组与援用范例数组显现转换,个中二者是父类与子类的关联(维数要雷同)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { class teacher { } class student:teacher //studnet继续teacher { } static void Main(string[] args) { teacher[] teac = new teacher[5]; student[] stu = new student[5]; stu = (student[])teac; //显现转换 } } }
假如换成下面的数组就不可
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { static void Main(string[] args) { double[] n_double = new double[5]; float[] n_float = new float[5]; n_float = (float[])n_double; //这里失足啦 } } }
从System.Array到数组范例 (array 是一切数组范例的基类)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { static void Main(string[] args) { Array arr = new Array[5]; //定义一个Array范例的数组并初始化 double[] d = new double[5]; d = (double[])arr; //显现转换 } } }
从System.Delegate到代表(托付)范例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { public static delegate int mydele(); //声明一个托付 class DE : Delegate //定义一个继续于Delegate 的类DE { } static void Main(string[] args) { Delegate MY =new DE(); // 将Delegate 抽象类间接实例化 mydele my = (mydele)MY; //显现转换 } } }
以上就是 C#进修日志17---显现范例转换具体用例的内容,更多相关内容请关注ki4网(www.ki4.cn)!