标签:c#
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) { ushort destinationVar; ushort destinationVar2; char SourceVar=‘a‘; destinationVar = SourceVar; //类型转换:隐式转换,将字符型转换为16进制无符号整型; Console.WriteLine("destinationVar={0}",destinationVar); //类型转换:显式转换,将字符型转换为16进制无符号整型; destinationVar2= Convert.ToUInt16(SourceVar); Console.WriteLine("destinationVar2={0}", destinationVar2); //溢出检查,byte范围(minsize~maxsize: 0~255) byte destinationVar3; short sourceVar = 7; destinationVar3 = (byte)sourceVar; Console.WriteLine("destinationVar3={0}", destinationVar3); //溢出检查,捕捉异常 byte destinationVar4; short sourceVar2 = 281; try { destinationVar4 = checked((byte)sourceVar2); Console.WriteLine("sourceVar2 val={0}", sourceVar2); Console.WriteLine("destinationVar4={0}", destinationVar4); } catch { Console.WriteLine("memory overflow."); } Console.ReadLine(); } } }
本文出自 “Ricky's Blog” 博客,请务必保留此出处http://57388.blog.51cto.com/47388/1650545
标签:c#
原文地址:http://57388.blog.51cto.com/47388/1650545