C# Learning Diary 16----Specific use cases of implicit conversion

黄舟
Release: 2017-01-21 15:02:09
Original
1701 people have browsed it

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
  • Implicit enumeration conversion
  • Implicit reference conversion
  • Implicit numerical conversion:
  • Implicit numerical conversion includes the following types:

    From sbyte type to short, int, long, float, double, decimal type;
  • From byte type to short, ushort, int, uint, long, ulong, float, double, decimal type;
  • From short type to int, long, flaot, double, and decimal types;
  • From ushort type to int, uint, long, ulong, flaot, double, and decimal types;
  • From int type to long, flaot, double, decimal type;
  • From uint type to long, ulong, flaot, double, decimal type;
  • From long type to float, double, decimal type;
  • From ulong type to float, double, decimal type;
  • From char type to ushort, int, uint, long, ulong, flaot, double, decimal type;
  • From float type to double type;
  • Let’s summarize after writing so much. In a nutshell, it is conversion from low-precision type to high-precision type (because precision and data information are not lost), but there is no implicit conversion from high-precision type to low-precision type (possibly Some information will be lost and it is not safe). For the precision and range of types, please refer to C# Learning Diary 04. What should be reminded here is that there is no implicit conversion from other types to the Char type.

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);   
  
              
        }  
    }  
}
Copy after login

Result:

C# Learning Diary 16----Specific use cases of implicit conversionAs 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);  
              
        }  
    }  
}
Copy after login

The output result is:

  Sunday
Copy after login

If we put day = 0 in the above code, it should be day = 1 Compile The processor will give an error.

Implicit reference conversion:

    Conversion from any reference type to object type;
  • (Person p = new Person())

  • Conversion from class type s to class type t, where s is a derived class of 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;        //将子类隐式转换为父类  
                  
                  
            }  
        }  
    }
    Copy after login
  • From class Conversion of type s to interface type t, where class s implements interface t; (the content about interface (interface) will be written later, use it to only declare methods but not define methods)
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(接口)隐式转换  
              
        }  
    }  
}
Copy after login

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;    //实现子接口到父借口隐式转换  
              
        }  
    }  
}
Copy after login

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;    //实现隐式转换  
              
        }  
    }  
}
Copy after login

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;  //这里报错啦  
              
        }  
    }
Copy after login

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  
        }  
    }
Copy after login

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 隐式转换  
        }  
    }  
}
Copy after login

以上就是 C#学习日记16----隐式转换具体用例的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template