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?