日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/7/25 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ylbtech-LanguageSamples-UserConversions(用户定义的转换) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-UserConversions(用戶定義的轉(zhuǎn)換)

?

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

“用戶定義的轉(zhuǎn)換”示例

本示例演示如何定義與類或結(jié)構(gòu)之間的轉(zhuǎn)換,以及如何使用此類轉(zhuǎn)換。有關(guān)更多信息,請(qǐng)參見(jiàn)轉(zhuǎn)換運(yùn)算符(C# 編程指南) 。

安全說(shuō)明

提供此代碼示例是為了闡釋一個(gè)概念,它并不代表最安全的編碼實(shí)踐,因此不應(yīng)在應(yīng)用程序或網(wǎng)站中使用此代碼示例。對(duì)于因?qū)⒋舜a示例用于其他用途而出現(xiàn)的偶然或必然的損害,Microsoft 不承擔(dān)任何責(zé)任。

在 Visual Studio 中生成并運(yùn)行“用戶定義的轉(zhuǎn)換”示例

  • 在“解決方案資源管理器”中,右擊“Conversion1”項(xiàng)目并單擊“設(shè)為啟動(dòng)項(xiàng)目”。

  • 在“調(diào)試”菜單上,單擊“開(kāi)始執(zhí)行(不調(diào)試)”。

  • 對(duì) Conversion2 重復(fù)上述步驟。

  • 從命令行生成并運(yùn)行“用戶定義的轉(zhuǎn)換”示例

  • 使用“更改目錄”命令轉(zhuǎn)到“Conversion1”目錄。

  • 鍵入以下命令:

    csc conversion.cs conversion
  • 使用“更改目錄”命令轉(zhuǎn)到“Conversion2”目錄。

  • 鍵入以下命令:

    csc structconversion.cs structconversion
  • 1.B,Conversion1 示例代碼(Sample Code)返回頂部

    1.B.1, conversion.cs

    // 版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。 // 此代碼的發(fā)布遵從 // Microsoft 公共許可(MS-PL,http://opensource.org/licenses/ms-pl.html)的條款。 // //版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。// conversion.cs using System;struct RomanNumeral {public RomanNumeral(int value) { this.value = value; }// 聲明從 int 到 RomanNumeral 的轉(zhuǎn)換。請(qǐng)注意// operator 關(guān)鍵字的使用。這是名為 // RomanNumeral 的轉(zhuǎn)換運(yùn)算符:static public implicit operator RomanNumeral(int value) {// 請(qǐng)注意,由于 RomanNumeral 聲明為結(jié)構(gòu),// 因此對(duì)該結(jié)構(gòu)調(diào)用 new 只是調(diào)用構(gòu)造函數(shù)// 而不是在堆上分配對(duì)象:return new RomanNumeral(value);}// 聲明從 RomanNumeral 到 int 的顯式轉(zhuǎn)換:static public explicit operator int(RomanNumeral roman){return roman.value;}// 聲明從 RomanNumeral 到// string 的隱式轉(zhuǎn)換: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;// 調(diào)用從 numeral 到 int 的顯式轉(zhuǎn)換。由于是顯式轉(zhuǎn)換, // 因此必須使用強(qiáng)制轉(zhuǎn)換:Console.WriteLine((int)numeral);// 調(diào)用到 string 的隱式轉(zhuǎn)換。由于沒(méi)有 // 強(qiáng)制轉(zhuǎn)換,到 string 的隱式轉(zhuǎn)換是可以考慮的 // 唯一轉(zhuǎn)換: Console.WriteLine(numeral);// 調(diào)用從 numeral 到 int 的顯式轉(zhuǎn)換, // 然后調(diào)用從 int 到 short 的顯式轉(zhuǎn)換:short s = (short)numeral;Console.WriteLine(s);} } View Code

    1.B.2,

    1.B.EXE,

    10 Conversion not yet implemented 10 請(qǐng)按任意鍵繼續(xù). . .

    1.B

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

    1.B.1, structconversion.cs

    // 版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。 // 此代碼的發(fā)布遵從 // Microsoft 公共許可(MS-PL,http://opensource.org/licenses/ms-pl.html)的條款。 // //版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。// 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;// 執(zhí)行從 RomanNumeral 到// BinaryNumeral 的轉(zhuǎn)換:binary = (BinaryNumeral)(int)roman;// 執(zhí)行從 BinaryNumeral 到 RomanNumeral 的轉(zhuǎn)換。// 不需要任何強(qiáng)制轉(zhuǎn)換:roman = binary;Console.WriteLine((int)binary);Console.WriteLine(binary);} } View Code

    1.B.2,

    1.B.EXE,

    10 Conversion not yet implemented 請(qǐng)按任意鍵繼續(xù). . .

    1.B,

    1.C,下載地址(Free Download)返回頂部

    ?

    作者:ylbtech
    出處:http://ylbtech.cnblogs.com/
    本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。

    轉(zhuǎn)載于:https://www.cnblogs.com/ylbtech/p/4197415.html

    總結(jié)

    以上是生活随笔為你收集整理的ylbtech-LanguageSamples-UserConversions(用户定义的转换)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。