C#的类型转换(1)之隐式和显式转换
2016-05-31 15:03
309 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//隐式转换
int i = 1;
long l = i;
C1 c1= new C2();//小范围向大范围转换
//显式转换---大范围向小的范围转换,或者说基类向子类
double d = 10.05;
int iFromD = (int)d;
//无法进行转换,使用处理异常:这个例子只是为了说明基类向子类转换的实例
C1 c11 = new C1();
try
{
C2 c2 = (C2)c11;
}
catch(Exception e) {
Console.WriteLine(e.Message);
}
}
}
class C1 { }
class C2 : C1 { }
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//隐式转换
int i = 1;
long l = i;
C1 c1= new C2();//小范围向大范围转换
//显式转换---大范围向小的范围转换,或者说基类向子类
double d = 10.05;
int iFromD = (int)d;
//无法进行转换,使用处理异常:这个例子只是为了说明基类向子类转换的实例
C1 c11 = new C1();
try
{
C2 c2 = (C2)c11;
}
catch(Exception e) {
Console.WriteLine(e.Message);
}
}
}
class C1 { }
class C2 : C1 { }
}
相关文章推荐
- C#学习笔记
- c# 进程间的通信实现之一简单字符串收发
- C# 图形的绘制矩形椭圆
- C#winform怎么弹出一个模式经度条对话框的实现方法
- C# 各种字符串格式
- C#导出Excel按照指定格式设置单元格属性值
- c# 打印的问题总结
- C# 读取EXCEL文件的三种经典方法
- C#知识点总结系列:C#中Delegate和Event以及它们的区别
- C#语法——const vs readonly
- c#winform选择文件,文件夹,打开指定目录方法
- C#Winform通过连接访问Excel文件
- C#学习笔记集合类型之数组(3)
- C#学习笔记集合类型之Hashtable、Dictionary和SortedList(2)
- C#学习笔记之ArrayList和List(1)
- C# Winform WebBrowser控件
- C#中TextBox水印提示的实现过程
- C# 中普通类、抽象类、接口之间的区别
- C#数字小写转大写
- C# 3.0新语言特性和改进(一)