For faster navigation, this Iframe is preloading the Wikiwand page for 通用型別系統.

通用型別系統

通用型別系統 (Common Type System) 定義了執行期引擎如何使用程式中的資料型別,以及如何配置資料在記憶體中的一種標準,依照此種標準所撰寫的程式語言,都可以在同一個執行期引擎中使用,因此它是跨語言支援的重要部份,亦即符合 CTS 規範的程式語言所撰寫出的程式,都可以在 CLR 中使用。

CTS與CLI規格都是由微軟所發展,目前已標準化為 ECMA 335 標準:「Common Language Infrastructure (CLI) Partitions I to VI.」,並且由 Microsoft 在 .NET Framework 中完全的實作。

CTS 的功能

[编辑]
  1. 建立一個可協助跨語言整合、型別安全與程式碼的高效能執行的框架規範。
  2. 提供可在許多程式語言中完全實作的物件導向模型。
  3. 定義程式語言必須遵行,以幫助在不同的程式語言中所實作的物件能夠與其他程式語言互動的規則。
  4. CTS 也定義了確保由不同程式語言所定義的資料類型能與其他程式語言互動的規則。

CTS 的類型

[编辑]

CTS 定義了兩種類型:

實值型別 (Value-type)

實值型別直接包含資料,而且實值型別的執行個體 (Instance) 不是在堆疊上配置,就是在結構中內嵌 (Inline) 配置。實值型別可為內建 (由執行階段實作)、使用者定義或列舉型別 (Enumeration)。

參考型別 (Reference-type)

儲存變數記憶體位址的參考,並且在堆積 (Heap) 上配置。參考型別可為自我描述型別、指標型別 (Pointer Type) 或介面型別 (Interface Type)。參考型別的型別可從自我描述型別的值判斷出來。自我描述型別可再分為陣列和類別型別。類別型別包括使用者定義型別、Boxed 實值型別和委派。

Boxing 與 Unboxing

[编辑]

Boxing

[编辑]

所謂的打包 (Boxing),是指將實值型別轉換成參考型別的一種機制,如下列程式碼所示,程式並不一定要明確的告知編譯器要將實值型別轉換成參考,它自己會處理這個部份。

Int32 x = 10; 
object o = x ; // 隱含打包
Console.WriteLine("The Object o = {0}",o); // prints out 10

然而,一個 Int32 變數總能夠使用類似下列的方式來外顯打包:

Int32 x = 10; 
object o = (object) x; // 外顯打包(明確指定)
Console.WriteLine("The object o = {0}",o); // prints out 10

Unboxing

[编辑]

所謂的解包 (Unboxing),是指將參考型別反轉回實值型別,如下列程式所示。

Int32 x = 5; 
object o = x; // 隱含打包
x = o; // 隱含解包

参考文献

[编辑]

外部連結

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
通用型別系統
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?