“用户定义的转换”示例

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

安全说明

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

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

  1. (-:In Solution Explorer, right-click the Conversion1 project and click Set as StartUp Project.)=100%(在解决方案资源管理器中,右击“Conversion1”项目并单击“设为启动项目”。:-)

  2. (-:On the Debug menu, click Start Without Debugging.)=100%(在“调试”菜单上单击“开始执行(不调试)”。:-)

  3. (-:Repeat the preceding steps for Conversion2.)=100%(对于 Conversion2,请重复前面的步骤。:-)

(-:To build and run the User-Defined Conversions samples from the Command Line)=100%(从命令行生成并运行“用户定义的转换”示例:-)

  1. (-:Use the Change Directory command to change to the Conversion1 directory.)=100%(使用“更改目录”命令转到“Conversion1”目录。:-)

  2. (-:Type the following:)=100%(键入以下命令::-)

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

  4. 键入以下命令:

    csc structconversion.cs
    structconversion