0

0

C#学习日记17---显示类型转换具体用例

黄舟

黄舟

发布时间:2017-01-21 15:07:06

|

1819人浏览过

|

来源于php中文网

原创

   在c#的类型转换中,除了上一篇中介绍到的 隐式类型转换 外还有一种需要我们声明的类型转换-----显示类型转换.

      显示类型转换,又叫强制类型转换,在进行转换的时候它需要我们明确的指定转换类型.  比如,当我们把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);  
              
        }  
    }  
}

运行结果:

艾绘
艾绘

艾绘:一站式绘本创作平台,AI智能绘本设计神器!

下载

640.png

对比发现当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);  
              
        }  
    }  
}

运行结果:

641.png

显示引用转换:

从对象到任何引用类型的转换;

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---显示类型转换具体用例的内容,更多相关内容请关注PHP中文网(www.php.cn)!

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

178

2026.01.28

包子漫画在线官方入口大全
包子漫画在线官方入口大全

本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。

35

2026.01.28

ao3中文版官网地址大全
ao3中文版官网地址大全

AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。

79

2026.01.28

php怎么写接口教程
php怎么写接口教程

本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。

2

2026.01.28

php中文乱码如何解决
php中文乱码如何解决

本文整理了php中文乱码如何解决及解决方法,阅读节专题下面的文章了解更多详细内容。

4

2026.01.28

Java 消息队列与异步架构实战
Java 消息队列与异步架构实战

本专题系统讲解 Java 在消息队列与异步系统架构中的核心应用,涵盖消息队列基本原理、Kafka 与 RabbitMQ 的使用场景对比、生产者与消费者模型、消息可靠性与顺序性保障、重复消费与幂等处理,以及在高并发系统中的异步解耦设计。通过实战案例,帮助学习者掌握 使用 Java 构建高吞吐、高可靠异步消息系统的完整思路。

8

2026.01.28

Python 自然语言处理(NLP)基础与实战
Python 自然语言处理(NLP)基础与实战

本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。

24

2026.01.27

拼多多赚钱的5种方法 拼多多赚钱的5种方法
拼多多赚钱的5种方法 拼多多赚钱的5种方法

在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。

122

2026.01.26

edge浏览器怎样设置主页 edge浏览器自定义设置教程
edge浏览器怎样设置主页 edge浏览器自定义设置教程

在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。

72

2026.01.26

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
C# 教程
C# 教程

共94课时 | 7.8万人学习

C 教程
C 教程

共75课时 | 4.3万人学习

C++教程
C++教程

共115课时 | 14.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号