ホームページ  >  記事  >  バックエンド開発  >  C#学習日記17---型変換の具体的な使用例を紹介

C#学習日記17---型変換の具体的な使用例を紹介

黄舟
黄舟オリジナル
2017-01-21 15:07:061551ブラウズ

C#の型変換には、前回の記事で紹介した暗黙的な型変換の他に、宣言が必要な型変換-----明示的な型変換があります

「明示的な型変換」とも呼ばれます。強制型変換 では、変換時に変換型を明示的に指定する必要があります。たとえば、long 型を int 型に変換する場合、この変換は精度を失う変換であるため、システムは暗黙的な変換を自動的に実行しません。したがって、強制変換する必要があります:

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

表示タイプの変換は、次のような 2 つのタイプには当てはまりません:

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

したがって、表示タイプの変換には特定のルールがあります:

  • 表示数値変換。

    表示列挙変換;
  • 表示参照変換;
  • 表示変換は常に成功するとは限らず、情報の損失が発生する可能性があります (型、範囲、精度も異なるため)。詳細については、を参照してください。表示変換にはすべての Impical 変換が含まれるため、次のような表示変換形式として非表示変換を記述することもできます。 R
            int i = 6000;
                                long l = (long)i;    //等价于 long l = i;
    E
  • 表示値変換:

表示値変換。これは、値の型間の変換を指します。値の型には次の規則があります:

sbyte から byte、ushort、uint、ulong、char 型
  • byte から sbyte、char 型へ。 、byte、ushort、uint、ulong、char 型;
  • ushort から sbyte、byte、short、char 型へ;

  • int から sbyte、byte、short、ushort、uint、ulong、char 型へ。

  • uintからsbyte、byte、short、ushort、int、char型へ;

  • longからsbyte、byte、short、ushort、int、uint、ulong、char型へ

  • ulongからsbyte、byte、short、ushort、int、uint、long、char 型へ

  • char から sbyte、byte、short 型へ

  • float から sbyte、byte、short、ushort、int、uint へ、long、ulong、char、10 進数型

  • double から sbyte、byte、short、ushort、int、uint、long、ulong、float、char、10 進数型へ

  • 10 進数から sbyte、byte 、short、ushort、int、uint、long、ulong、Float、char、double 型;

  • ここまで書いたら、高精度から低精度への変換です。または丸め変換の例を書いてみましょう:

    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);  
                  
            }  
        }  
    }
  • 実行結果:

比較すると、double データ範囲が float の有効な値の範囲を超える場合、表示変換中に 9 桁目が丸められることがわかりました。 int型に変換すると整数部分のみが残ります。


表示列挙型変換:

C#学習日記17---型変換の具体的な使用例を紹介 表示列挙型変換には次の内容が含まれます:

sbyte、byte、short、ushort、int、uint、long、ulong、float、char、double、10 進数型から任意の列挙型へ型;

任意の列挙型から sbyte、byte、short、ushort、int、uint、long、ulong、float、char、double、10 進数型へ。

  • 例を書きます:

    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);  
                  
            }  
        }  
    }
  • 実行結果:
  • 参照変換を表示:

    オブジェクトから任意の参照型への変換;
  • 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类  
                  
            }  
        }  
    }
クラス型sからクラス型tの変換(sは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;  //将父类转换为子类  
              
        }  
    }  
}

クラス型 s からインターフェース t への変換。ただし、s はシールされたクラスではなく、t を実装しません (インターフェース (インターフェース) に関する内容は後で書きます。メソッドを宣言するだけですが、メソッドを定義していません)

System を使用;

System.Collections.Generic を使用;C#学習日記17---型変換の具体的な使用例を紹介System.Linq を使用;

System.Text を使用;

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

インターフェイス型 s からクラス型 t への変換 (t はシールされていません)クラスであり、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;  // 显示转换  
                          
        }  
    }  
}

インターフェイス型 s からインターフェイス型 t への変換 (s は 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;  // 显示转换  
                          
        }  
    }  
}

参照型配列と参照型配列は、両方が親である場合の変換を示します。クラスとサブクラス間 (次元は同じである必要があります)

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;      //显示转换   
              
        }  
    }  
}

以下の配列に変更すると動作しません
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;     //这里出错啦  
              
        }  
    }  
}

System.Array から配列型へ (配列はすべての配列型の基本クラスです)
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;    //显示转换  
        }  
    }  
}

System.Delegate から代表 (デリゲート) 型へ

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;  //显示转换  
        }  
    }  
}

以上は C# 学習日記 17 の内容です ---型変換の具体的な使用例については、PHP 中国語 Web サイトをご覧ください。 (www.php.cn)!

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。