偶的第一篇译文(上):Data Types in C#

来源:互联网 时间:1970-01-01

[前言]:昨天在看小新0574的一篇post的时候,发现自己对于数据类型的了解还是很肤浅的,于是搜索了一下,找到了这篇来自DotNetJunkies文章。个人觉得文章写得很不错,条理清晰而且有一定深度,故将其翻译,以搏诸位一哂了。
原文:Data Type in C#
作者:Wajahat Abbas
C# 的数据类型
在这篇文章中,让我们来了解一下C#的主要内容——C#的数据类型,同时,我们也将了解C#中定义的值类型和引用类型。为了能够理解得更加透彻,我们在学习的过程中,将相应编写一些范例程序。通过这些程序,你会轻松地理解这些内容。
拥有不同编程语言背景的程序员一定已经体验过数据类型的强大功能了,而VB 6.0的程序员更是如此。因为VB6.0提供了非常丰富的数据类型:从布尔型到固有的字符串类型,再到日期/时间类型;另一方面,像C、C++语言则会要求程序员自己将原生类型糅合在一起去构建特有的数据类型。原生类型组合起来就得到了类、结构体或者复杂的类库,如MFC。

CTS (Common Type System)
在C#出现之后,C程序员也有了自己的快速应用开发环境(RAD)。这可能是通过CTS(Common Type System,通用类型体系)去实现的。C#跟其他语言一样支持CTS。
Micorsoft所描述的CTS的核心功能列示如下:

建立一个支持跨语言集成、类型安全并且拥有高效的代码执行效率的框架; 提供一个面向对象模型模型。该模型可以支持很多语言的完整实现; 定义一系列的语言必须遵循的规则,以保证由不同语言实现的对象之间可以进行交互。 正如你刚看到的,在CTS提供的核心功能中,最重要的功能是保证了跨语言的集成。通过这个功能,程序员可以利用.NET Framework支持的任何一种语言去创建对象,并且在任何一种语言中使用这些对象。而这个功能的实现正是由于定义了数据类型集合的CTS。

CTS 概览
CTS可以分为两个主要类型。而两个主要类型还可以继续细分为更多的子类型。

值类型
值类型在堆栈(stack)存储数据。堆栈是一个存储像int、float等等这些固定长度数据的地方。每一个程序都各自拥有自己的堆栈,而其他程序是不能共享的。你可以认为堆栈是在函数调用的时候获得的内存块,所有调用函数的局部变量会被压入程序堆栈中,随后被依次取出。我记得一次在VB中创建COM的经历:那时我用了太多的递归函数,结果得到了一个"Out of Stack Space"的错误。这个错误是由堆栈溢出导致,因为在递归函数中,会有很多变量进入堆栈,从而导致堆栈溢出。因此,在递归函数中使用变量一定要小心谨慎。
如果我们将一个变量赋值给另外一个变量,在堆栈中则会创建两个不同的复本,尽管它们的数据是一样的,因此,在堆栈中分配了两块空间。值类型正如它名字所暗示的,在赋值过程中是将一个变量的值复制到另外一个变量,如果在程序中一个变量被修改了,而另一个变量并不会相应的改变。这就是值类型的主体:变量之间赋值只是复制值,而一个变量的改变对另一个变量不会有任何影响。

引用类型
引用类型在堆(heap)中存储数据。堆是另外一个我们能够在.NET Framework中存储数据的地方。引用类型存储类型(对象)的引用,而不是值本身。所以一个对象被修改了将影响到这个对象的其他引用。引用类型的类型可以由自描述类型、指针或者接口类型的值来决定。其中,自描述类型可以进一步分为:数组和类类型。

本篇原文:

In this article we will learn about one of the main topics in C# -- Data Types in C#. We will also learn about the value and reference types defined in C#. To understand properly we will learn with creating some samples program by which you can understand it easily.

Programmers from different language backgrounds have experienced the power of Data Types, especially VB 6.0 developers had a very broad range of data types range from Boolean, intrinsic String type to date/time types, on the other hand languages like C, C++ forced the programmers to construct their own data types by gluing primitives together into classes or structs, or complex propriety libraries such as MFC.

CTS (Common Type System)

After C#, C programmers too have their own Rapid Application Development environment. This is possible by CTS (Common Type System), C# supports the CTS like other languages.

The core functions of CTS as described by the Microsoft are as following:

Establishes a Framework that enables cross-language integrations, type safety, and high performance code execution.
Provides an Object Oriented Model that supports the complete implementation of many programming languages.
Define set of rules that languages must follow, which helps ensures that object written in different languages can interact with each other.

As you have read the core functionalities provided by CTS, the most important function is that it ensures cross-language integration by which programmers can create objects in any language which is supported by .Net Framework and can use these objects in any .Net languages; it is due to CTS which defines a set of Data types.

CTS Overview

The CTS is classified into two main types, which may be further classified into more subtypes.

Value Type

Value types store the data on the stack, STACK is a place where data stores in fixed length such as int, float, etc. Every program has its own stack and no other program shares it, and it allocates on program execution. You can imagine Stack as a block acquired in memory and when the function of the program is called, all local variables to the calling function are pushed onto the program stack, to be latter popped off and retrieved. I remember one of my experiences in creating an COM in VB. I was using so many recursive functions and got the error "Out of Stack Space". This was due to Stack overflow, because in recursive functions there were so many variables which popped up onto the stack that the Stack overflowed so one must careful using Variables in recursive functions.

If we are assigning one value variable to the other variable, it creates two distinct copies of the same data on the stack. So now in the Stack there are two spaces allocated. The value type as it name implies copies the value to the other variable not the reference of the other value. In Value type if the first variable modifies in the program it doesn't modify the other variable value. This is the main theme of Value type that it copies the value and doesn't makes effect on the other variable if the first variable got changed.

Reference Type

Reference types stores the data on the heap. This is the second place where we can store the data in the .NET Framework. Reference type stores the reference of the type, not the value. So if the value of the first object modified it will also effect the other type. The type of reference type can be determined from values of self describing types, pointers, or interfaces types. Self describing types are further classified into array and class types.



相关阅读:
Top