您的位置:首页 > 编程语言 > C#

C#学习日记16----隐式转换具体用例

2015-10-06 20:28 471 查看
             经过前面的学习C#中基本的数据类型就介绍的差不多了,下面就学习下类型之间的互相转换.C# 中类型转换可以分为2类: 隐式转换 和 显式转换.

隐式转换:

         隐式转换是系统默认的转换,不需要申明就可以进行转换。在隐式转换过程中,编译器无需对转换进行检查就能够安全的执行转换,比如从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 隐式转换
}
}
}

 

 

常用到的就介绍这么多了,需要补充请私信我,有关委托我后面会写到的,请继续支持HC66,感谢你的阅读^_^,

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息