After the previous study of the basic data types in C#, we have almost introduced it. Next, we will learn about the mutual conversion between types. Type conversion in C# can be divided into two categories: implicit conversion and explicit conversion.
Implicit conversion:
Implicit conversion is the default conversion of the system and can be converted without declaration. During the implicit conversion process, the compiler can safely perform the conversion without checking the conversion. For example, converting from int type to long type is an implicit conversion. Implicit conversion generally will not fail, and information will not be lost during the conversion process.
For example: int i = 100;
long a = i; // Automatically convert int type without declaration Convert to long type
Implicit conversion is not true for any two types. For example, if we implicitly convert the above long type to int type, it will not succeed: 100;
int i = a; //The compiler will report an error
Therefore, the implicit conversion has the following rules:
Implicit numerical conversion example:
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); } } }
Result:
As can be seen from this example, in time The use of type conversion is still very important.
Implicit enumeration conversion:
Implicit enumeration conversion allows decimal 0 to be converted to any enumeration type. Note that it can only convert 0 , there is no such implicit conversion for other integers, see the following example:
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); } } }
The output result is:
Sunday
If we put day = 0 in the above code, it should be day = 1 Compile The processor will give an error.
Implicit reference conversion:
(Person p = new Person())
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; //将子类隐式转换为父类 } } }
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(接口)隐式转换 } } }
From interface type s Conversion to interface type t, where t is the parent interface of 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; //实现子接口到父借口隐式转换 } } }
Conversion from reference type array s to reference type array t, where s is a derived class of t, and the array dimensions must be the same;
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; //实现隐式转换 } } }
It should be reminded here that if the reference type array is a value type array, the following code will report an error:
class Program { static void Main(string[] args) { int[] n_int = new int[10]; double[] n_doubel = new double[10]; n_doubel = n_int; //这里报错啦 } }
Conversion from array type to System.Array; (Array is the base of all arrays Class reference previous article ^_^)
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 } }
Conversion from any representative type to System.Delegate; (will write about delegate later)
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 隐式转换 } } }
以上就是 C#学习日记16----隐式转换具体用例的内容,更多相关内容请关注PHP中文网(www.php.cn)!