• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • C# 程序員參考--用戶定義的轉換教程

    發表于:2007-07-14來源:作者:點擊數: 標簽:
    C# 程序員 參考--用戶定義的轉換教程: 本教程展示如何定義轉換以及如何在類或結構之間使用轉換。 示例文件 請參見“用戶定義的轉換”示例以下載和生成本教程中討論的示例文件。 教程 C# 允許程序員在類或結構上聲明轉換,以便可以使類或結構與其他類或結構
    C# 程序員參考--用戶定義的轉換教程:

    本教程展示如何定義轉換以及如何在類或結構之間使用轉換。

    示例文件

    請參見“用戶定義的轉換”示例以下載和生成本教程中討論的示例文件。

    教程

    C# 允許程序員在類或結構上聲明轉換,以便可以使類或結構與其他類或結構或者基本類型相互進行轉換。轉換的定義方法類似于運算符,并根據它們所轉換到的類型命名。

    在 C# 中,可以將轉換聲明為 implicit(需要時自動轉換)或 explicit(需要調用轉換)。所有轉換都必須為 static,并且必須采用在其上定義轉換的類型,或返回該類型。

    本教程介紹兩個示例。第一個示例展示如何聲明和使用轉換,第二個示例演示結構之間的轉換。

    示例 1

    本示例中聲明了一個 RomanNumeral 類型,并定義了與該類型之間的若干轉換。

    // conversion.csusing System;struct RomanNumeral{    public RomanNumeral(int value)     {        this.value = value;     }    // Declare a conversion from an int to a RomanNumeral. Note the    // the use of the operator keyword. This is a conversion     // operator named RomanNumeral:    static public implicit operator RomanNumeral(int value)     {       // Note that because RomanNumeral is declared as a struct,        // calling new on the struct merely calls the constructor        // rather than allocating an object on the heap:       return new RomanNumeral(value);    }    // Declare an explicit conversion from a RomanNumeral to an int:    static public explicit operator int(RomanNumeral roman)    {       return roman.value;    }    // Declare an implicit conversion from a RomanNumeral to     // a string:    static public implicit operator string(RomanNumeral roman)    {       return("Conversion not yet implemented");    }    private int value;}class Test{    static public void Main()    {        RomanNumeral numeral;        numeral = 10;// Call the explicit conversion from numeral to int. Because it is// an explicit conversion, a cast must be used:        Console.WriteLine((int)numeral);// Call the implicit conversion to string. Because there is no// cast, the implicit conversion to string is the only// conversion that is considered:        Console.WriteLine(numeral); // Call the explicit conversion from numeral to int and // then the explicit conversion from int to short:        short s = (short)numeral;        Console.WriteLine(s);    }}

    輸出

    10Conversion not yet implemented10

    示例 2

    本示例定義 RomanNumeralBinaryNumeral 兩個結構,并演示二者之間的轉換。

    // structconversion.csusing System;struct RomanNumeral{    public RomanNumeral(int value)     {        this.value = value;     }    static public implicit operator RomanNumeral(int value)    {        return new RomanNumeral(value);    }    static public implicit operator RomanNumeral(BinaryNumeral binary)    {        return new RomanNumeral((int)binary);    }    static public explicit operator int(RomanNumeral roman)    {         return roman.value;    }    static public implicit operator string(RomanNumeral roman)     {        return("Conversion not yet implemented");    }    private int value;}struct BinaryNumeral{    public BinaryNumeral(int value)     {        this.value = value;    }    static public implicit operator BinaryNumeral(int value)    {        return new BinaryNumeral(value);    }    static public implicit operator string(BinaryNumeral binary)    {        return("Conversion not yet implemented");    }    static public explicit operator int(BinaryNumeral binary)    {        return(binary.value);    }    private int value;}class Test{    static public void Main()    {        RomanNumeral roman;        roman = 10;        BinaryNumeral binary;        // Perform a conversion from a RomanNumeral to a        // BinaryNumeral:        binary = (BinaryNumeral)(int)roman;        // Performs a conversion from a BinaryNumeral to a RomanNumeral.        // No cast is required:        roman = binary;        Console.WriteLine((int)binary);        Console.WriteLine(binary);    }}

    輸出

    10Conversion not yet implemented

    代碼討論

    • 在上個示例中,語句
      binary = (BinaryNumeral)(int)roman;

      執行從 RomanNumeralBinaryNumeral 的轉換。由于沒有從 RomanNumeralBinaryNumeral 的直接轉換,所以使用一個轉換將 RomanNumeral 轉換為 int,并使用另一個轉換將 int 轉換為 BinaryNumeral。

    • 另外,語句
      roman = binary;

      執行從 BinaryNumeral RomanNumeral 的轉換。由于 RomanNumeral 定義了從 BinaryNumeral 的隱式轉換,所以不需要轉換。

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>