ylbtech-LanguageSamples-UserConversions(用户定义的转换)

ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-UserConversions(用户定义的转换)

 

1.A,示例(Sample) 返回顶部

“用户定义的转换”示例

本示例演示如何定义与类或结构之间的转换,以及如何使用此类转换。有关更多信息,请参见转换运算符(C# 编程指南) 。

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行“用户定义的转换”示例

  1. 在“解决方案资源管理器”中,右击“Conversion1”项目并单击“设为启动项目”。

  2. 在“调试”菜单上,单击“开始执行(不调试)”。

  3. 对 Conversion2 重复上述步骤。

从命令行生成并运行“用户定义的转换”示例

  1. 使用“更改目录”命令转到“Conversion1”目录。

  2. 键入以下命令:

    csc conversion.cs
    
    conversion
  3. 使用“更改目录”命令转到“Conversion2”目录。

  4. 键入以下命令:

    csc structconversion.cs
    
    structconversion
1.B,Conversion1 示例代码(Sample Code)返回顶部

1.B.1, conversion.cs

ylbtech-LanguageSamples-UserConversions(用户定义的转换)
// 版权所有(C) Microsoft Corporation。保留所有权利。

// 此代码的发布遵从

// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。

//

//版权所有(C) Microsoft Corporation。保留所有权利。



// conversion.cs

using System;



struct RomanNumeral

{

    public RomanNumeral(int value) 

    { 

       this.value = value; 

    }

    // 声明从 int 到 RomanNumeral 的转换。请注意

    // operator 关键字的使用。这是名为 

    // RomanNumeral 的转换运算符:

    static public implicit operator RomanNumeral(int value) 

    {

       // 请注意,由于 RomanNumeral 声明为结构,

       // 因此对该结构调用 new 只是调用构造函数

       // 而不是在堆上分配对象:

       return new RomanNumeral(value);

    }

    // 声明从 RomanNumeral 到 int 的显式转换:

    static public explicit operator int(RomanNumeral roman)

    {

       return roman.value;

    }

    // 声明从 RomanNumeral 到

    // 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;



// 调用从 numeral 到 int 的显式转换。由于是显式转换,

// 因此必须使用强制转换:

        Console.WriteLine((int)numeral);



// 调用到 string 的隐式转换。由于没有

// 强制转换,到 string 的隐式转换是可以考虑的

// 唯一转换:

        Console.WriteLine(numeral);

 

// 调用从 numeral 到 int 的显式转换,

// 然后调用从 int 到 short 的显式转换:

        short s = (short)numeral;



        Console.WriteLine(s);

    }

}
View Code

1.B.2,

1.B.EXE,

10

Conversion not yet implemented

10

请按任意键继续. . .

1.B

1.B,Conversions2 示例代码2(Sample Code)返回顶部

1.B.1, structconversion.cs

ylbtech-LanguageSamples-UserConversions(用户定义的转换)
// 版权所有(C) Microsoft Corporation。保留所有权利。

// 此代码的发布遵从

// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。

//

//版权所有(C) Microsoft Corporation。保留所有权利。



// structconversion.cs

using 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;

        // 执行从 RomanNumeral 到

        // BinaryNumeral 的转换:

        binary = (BinaryNumeral)(int)roman;

        // 执行从 BinaryNumeral 到 RomanNumeral 的转换。

        // 不需要任何强制转换:

        roman = binary;

        Console.WriteLine((int)binary);

        Console.WriteLine(binary);

    }

}
View Code

1.B.2,

1.B.EXE,

10

Conversion not yet implemented

请按任意键继续. . .

1.B,

1.C,下载地址(Free Download)返回顶部

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

你可能感兴趣的:(conversion)