Home  >  Article  >  Backend Development  >  C# Learning Diary 17---Show specific use cases of type conversion

C# Learning Diary 17---Show specific use cases of type conversion

黄舟
黄舟Original
2017-01-21 15:07:061551browse

In the type conversion of C#, in addition to the implicit type conversion introduced in the previous article, there is also a type conversion that needs to be declared by us ----- explicit type conversion.

Display type conversion , also called forced type conversion, it requires us to explicitly specify the conversion type when performing conversion. For example, when we convert the long type to the int type, since this conversion is a conversion that loses precision, the system will not automatically perform implicit conversion. formula conversion, so forced conversion is required:

      long l = 6000;
                  int i = (int)l;    //需要用在 ()里面声明转换类型

Display type conversion is not true for any two types, such as:

       int i = 6000;
                  string i = (string)i;    //这里会报错

Therefore, display type conversion also has certain rules:

  • Display numeric conversion;

  • Display enumeration conversion;

  • Display reference conversion;

Display conversion is not always successful, and may often cause the loss of information (because the types are different, the range and precision are also different. For details, please refer to the data type). Display conversion includes all implicit conversions. Therefore, implicit conversion can also be written in the form of explicit conversion, such as:

        int i = 6000;
                            long l = (long)i;    //等价于 long l = i;

Display numerical conversion:

Display numerical conversion refers to the conversion between value type and value type, as follows Rules:

  • From sbyte to byte, ushort, uint, ulong, char type;

  • From byte to sbyte, char type;

  • From short to sbyte, byte, ushort, uint, ulong, and char types;

  • From ushort to sbyte, byte, short, and char types ;

  • From int to sbyte, byte, short, ushort, uint, ulong, char type;

  • From uint to sbyte, byte, short, ushort, int, char types;

  • From long to sbyte, byte, short, ushort, int, uint, ulong, char types;

  • From ulong to sbyte, byte, short, ushort, int, uint, long, char types;

  • From char to sbyte, byte, short type;

  • From float to sbyte, byte, short, ushort, int, uint, long, ulong, char, decimal type;

  • From double to sbyte, byte, short, ushort, int, uint, long, ulong, float, char, decimal type;

  • from decimal to sbyte, byte, short, ushort, int, uint, long, ulong, Float, char, double types;

After writing so much, let’s summarize it. It is the conversion from high precision to low precision. It may be a retaining conversion or a rounding conversion. Write Example:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        static void Main(string[] args)  
        {  
            double n_double = 1.73456789;  
            float n_float = (float)n_double;  //显示转换 float的有效为只有8位(.也是一位)所以从第9位四舍五入  
  
            int n_int = (int)n_double; //只保留整数  
  
            Console.WriteLine("n_float = {0}\nn_int = {1}",n_float,n_int);  
              
        }  
    }  
}

Running results:


C# Learning Diary 17---Show specific use cases of type conversion

## Comparison found that when the double data range exceeds the valid value range of float, it is displayed The 9th digit is rounded when converting, and only the integer part is retained when converting to int type.

Display enumeration conversion:

Display enumeration conversion includes the following contents:

  • From sbyte, byte, short, ushort, int, uint, long, ulong, float, char, double, decimal types to any enumeration type;

  • From any enumeration type to sbyte, byte, short, ushort, int, uint, long, ulong, float, char, double, decimal types;

  • From any enumeration type to any other enumeration type;

Write an example:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        enum weekday   //定义2个枚举  
        {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday }  
        enum Month  
        {Janurary=1,February,March,April,May,Jun,July }  
        static void Main(string[] args)  
        {  
            int n_int = 2;  
            double n_double = 3.0;  
            decimal n_decimal = 5m;  //声明decimal 类型要加m  
  
            weekday weki = (weekday)n_int;     //从int、double、decimal到枚举转换  
            weekday wekd = (weekday)n_double;  
            weekday wekde = (weekday)n_decimal;  
  
            weekday wek = weekday.Tuesday;   //枚举类型之间的转换  
            Month mon = (Month)wek;  
  
            int i = (int)wek;  //从枚举类型到int的转换  
            int t = (int)mon;  
            Console.WriteLine("n_int = {0}\nn_double = {1}\nn_decimal = {2}",weki,wekd,wekde);  
            Console.WriteLine("wek = {0}\nmon = {1}\nwek ={2}\tmon = {3}",wek,mon,i,t);  
              
        }  
    }  
}

Running result:

C# Learning Diary 17---Show specific use cases of type conversion##Display reference conversion:

From object to any reference type Conversion;

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        //定义2个类 teacher与man  
        class teacher  
        { }  
        class man  
        { }  
        static void Main(string[] args)  
        {  
            man per = new man();  //将man实例化一个对象per  
            object o = per;      //装箱  
            teacher p = (teacher)o;  // 将o显示转换为teacher类  
              
        }  
    }  
}

Conversion from class type s to class type t, where s is the base class of t;

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        class man   //定义一个基类  
        { }  
        class student:man  //student继承man  
        { }  
        static void Main(string[] args)  
        {  
            man per = new man();  //man实例化一个对象per  
            student stu = (student)per;  //将父类转换为子类  
              
        }  
    }  
}

Conversion from class type s to interface t, where s is not sealed Class, does not implement t; (The content of the interface will be written later, it only declares methods but does not define methods)

using System;

using System.Collections.Generic;

using System.Linq;
using System.Text;


namespace Test
{  
    class Program
    {
        public interface teacher  //定义一个接口 
        { }
        class student   //定义一个类
        { }
        static void Main(string[] args)
        {
            student stu = new student(); //实例化一个对象
            teacher tea = (teacher)stu;  // 显示转换
                        
        }
    }
}

Conversion from interface type s to class type t, where t is not a sealed class and does not implement s;

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        public interface man  //定义一个接口   
        { }  
        class teacher:man  //定义一个继承于man的类man  
        { }  
        class student   //定义一个新类  
        { }  
        static void Main(string[] args)  
        {  
            man teac=new teacher(); //间接实例化一个接口  
            student stu = (student)teac;  // 显示转换  
                          
        }  
    }  
}

Conversion from interface type s to interface type t, where s is not a sub-interface of t;

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        public interface man  //定义一个接口   
        { }  
        class teacher : man    //由接口派生一个类  
        { }  
        public interface person //定义一个接口  
        { }  
        class student:person   //由接口派生一个类  
        { }  
        static void Main(string[] args)  
        {  
            man teac=new teacher(); //间接实例化一个接口  
            person stu = (person)teac;  // 显示转换  
                          
        }  
    }  
}

Reference type array and reference type array display conversion, both of which are parent classes and subclasses The relationship (the dimensions must be the same)

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        class teacher  
        { }  
        class student:teacher  //studnet继承teacher  
        { }  
        static void Main(string[] args)  
        {  
            teacher[] teac = new teacher[5];  
            student[] stu = new student[5];  
            stu = (student[])teac;      //显示转换   
              
        }  
    }  
}

If you change to the following array, it will not work

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        static void Main(string[] args)  
        {  
           double[] n_double = new double[5];  
            float[] n_float = new float[5];  
            n_float = (float[])n_double;     //这里出错啦  
              
        }  
    }  
}

From System.Array to array type (array is the base class of all array types)

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
        static void Main(string[] args)  
        {  
           Array arr = new Array[5];   //定义一个Array类型的数组并初始化  
           double[] d = new double[5];  
           d = (double[])arr;    //显示转换  
        }  
    }  
}

From System.Delegate to representative (delegate) type

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{    
    class Program  
    {  
          
        public static delegate int mydele();  //声明一个委托  
  
        class DE : Delegate  //定义一个继承于Delegate 的类DE  
        { }  
        static void Main(string[] args)  
        {  
            Delegate MY =new DE();   // 将Delegate 抽象类间接实例化  
            mydele my = (mydele)MY;  //显示转换  
        }  
    }  
}

The above is the content of C# Learning Diary 17---Displaying specific use cases of type conversion. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!

Statement:
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