预定义类型
CTS定义了很多能够被C#编译器识别的数据类型,譬如整型,浮点型,字符型和布尔型。我们可以简单的通过为一个对象起个名字的方式去声明一个值类型变量,例如声明了一个布尔类型变量,如下代码所示:
bool b; //This declares a Boolean b on stack
b = false ; //This assign the value false to b;
另一个示例代码如下:
public class ValueTypes
{
public static void Main (string [] args)
{
byte bVar = 111;
int iVar = 786;
float fVar = 110;
Console.WriteLine("The Value of bVar is "+bVar);
Console.WriteLine("The Value of iVar is "+iVar);
Console.WriteLine("The Value of fVar is "+fVar);
Console.ReadLine();
}
}
当你执行这个程序的时候,所赋的值将会显示出来然后等待你按下回车来结束整个过程。我们可以看到在程序中我们声明了三个值类型变量。其中,第一个是字节型,而字节型实际上代表着CTS中的System.Byte类型,另外两个类型也可以在CTS中找到对应类型。与C和C++类似,你也可以在同一行中声明并且初始化变量。
复杂类型
我们还可以定义另外两种值类型:结构体(Structs)和枚举(Enumeration)。
结构体
结构体是一种特殊类型的类(class),因为结构体是值类型的,而不是引用类型。正如我们所了解到的值类型存在堆栈一样,结构体也存在堆栈上。这样,结构体能够十分有效的被复制和创建(对堆栈进行复制和访问本来就会比对堆进行相应的操作更有效)。
枚举
枚举是用户定义的整数类型。Enum关键字用于声明一个枚举类型。基本上,枚举类型的主要的功能是让我们的代码更加易于维护。变量只能被赋予合法的值,而不会理会值本身的含义。而枚举则会让我们的代码更清楚,因为枚举可以为整数创建一个伪名(deceptive names)。
下面的表格列示了所有值类型和它们在CTS中相应的类型及描述:
Name |
CTS Type |
Description |
byte |
System.Byte |
An 8-bits unsigned Value |
sbyte |
System.SByte |
An 8-bits signed Value |
int |
System.Int32 |
A 32-bit signed integer |
uint |
System.UInt32 |
A 32 bit unsigned integer |
short |
System.Int16 |
A 16-bit signed value |
ushort |
System.UInt16 |
A 16-bit unsigned value |
long |
System.Int64 |
A 64-bit signed value |
ulong |
System.UInt64 |
A 64-bit unsigned value |
float |
System.Single |
A single precision 32-bit |
double |
System.Double |
A double precision 64-bit |
decimal |
System.Decimal |
A 96-bit decimal value |
bool |
System.Boolean |
true and false |
char |
System.Char |
A single 16-bit character |
C#支持两种预定义的引用类型,对象和字符串。引用类型在堆中存储引用(这里的引用是指对象本身,而非对象的引用)。堆是另外一个存放引用而不是数据的地方。现在,在.NET中的堆被称为托管堆(Manged Heap),具有更强的可管理性。当你初始化一个进程的时候,运行时环境会保留一个毗邻的地址空间为进程所用。托管堆会维护一个指向堆中某个地址的指针,这个地址就是下一个对象在堆中创建所要分配内存空间的起始地址(此处为意译)。最初,这个指针指向的地址是托管堆的基地址。系统会为所有的引用类型在托管堆中分配内存。当程序创建第一个引用类型的时候,系统会为它在托管堆的基地址分配相应的内存。当应用程序创建下一个对象的时候,垃圾回收器会为这个对象在上一个被创建的对象的后续地址空间中分配内存。只要地址空间是可用的,垃圾回收器就会以这样的方式为新对象分配空间。
在C#中,你必须使用new关键字去初始化一个引用对象,也就是说,我们创建一个引用并且使用new关键字令这个引用指向在堆中的对象,如下所示:
Object myobj;
myobj = new Object();
以上的代码生成一个引用myobj,并且在堆中将其初始化。引用类型以及它们相应的CTS和描述列示如下:
Name |
CTS Type |
Description |
Object |
System.Object |
This is the root for all other types in CTS. |
String |
System.String |
Unicode character string |
至此,我们已经了解两种类型的基本概念了,接着就来编写一个包含了这两种数据类型的程序:
using System;
class ReferenceTypes
{
public int rWidth;
public int rHeight;
}
struct ValuesTypes
{
public int iWidth;
public int iHeight;
}
class Test
{
public static void Main(string [] args)
{
ValuesTypes objVal1 = new ValuesTypes();
objVal1.iWidth = 10;
objVal1.iHeight = 10;
ValuesTypes objVal2 = objVal1;
Console.WriteLine("Value of Val1 types before changing Val2 width ="+objVal1.iWidth+" and height = "+objVal1.iHeight);
objVal2.iWidth = 20;
objVal2.iHeight = 20;
Console.WriteLine("Value of Val1 types after changing Val2 width ="+objVal1.iWidth+" and height = "+objVal1.iHeight);
ReferenceTypes objRef1 = new ReferenceTypes();
objRef1.rWidth = 10;
objRef1.rHeight = 10;
ReferenceTypes objRef2 = objRef1;
Console.WriteLine("Value of Ref1 types before changing Ref2 width ="+objRef1.rWidth+" and height = "+objRef1.rHeight);
objRef2.rWidth = 20;
objRef2.rHeight = 20;
Console.WriteLine("Value of Ref1 types after changing Ref2 width ="+objRef1.rWidth+" and height = "+objRef1.rHeight);
Console.ReadLine();
}
}
因此,你已经从以上的程序发现,结构体是值类型,它们存储的是数据本身而不是对象的引用,变量间的改动对代码中的ValueTypes不会产生任何的效果。而在另一方面,引用类型则通过引用去修改并显示出被修改过的实际数据。
我希望你能理解如何在C#中使用数据类型。在这个指南中,我们已经了解到C#和CTS中的基本数据类型,在下一篇文章中将关注数据类型转换。
本篇原文:
C # Predefined Types
CTS defines a number of data types which can be recognized by the C# compiler. C# has fifteen of these predefined types in which thirteen are value and two are reference type.
Built-in Value Types
Built-in value type represents primitives, such as integer, floating point, characters and Boolean types.
To declare a value type we simply give the name of the object itself, like in the following code where we declare a Boolean type:
bool b; //This declares a Boolean b on stack
b = false ; //This assign the value false tp b;
As an example consider the following code:
When you execute the program, it will display the assigned values and wait for the carriage return to finish. In this program we have seen that we have declared three value types. The first one is byte which is actually represented as System.Byte in the CTS and so as the all others, like C and C++ you can also declare and initialize the variables into same line and while declaring it.
Complex Type
There are two more types of value types which we can define, these are Structs and Enumeration.
Structs
Struct is a special kind of class that is value type rather than a reference type. As we all know value types are stored on the Stack, structs also are stored on the Stack. In this way they can be copied and created efficiently (Stacks are more efficiently copied and accessed than Heap).
Enumeration
Enumerations are the user defined integer type. Enum keyword is used to declare an enumeration. Basically the main function is that it makes our code easier to maintain. Variables are assigned only legitimate values. The enumerator also makes our code cleaner by creating deceptive names for inetegers.
The following table represents all the value types, and its respective CTS type and description:
Reference Types
C# supports two predefined reference types, object and string. Reference types store references in a Heap. Heap is the second place where references can be store instead of data. In .NET the heap is now more managed and is called the Managed Heap. When you initialize a new process, the runtime reserves a contiguous region of address space for the process. The managed heap maintains a pointer to the address where the next object in the heap will be allocated. Initially this pointer is set to the managed heap's base address. All reference types are allocated on the managed heap. When an the application creates the first reference type, memory is allocated for the type at the base address of the managed heap. When the application creates the next object, the garbage collector allocates memory for it in the address space immediately following the first object. As long as address space is available, the garbage collector continues to allocate space for new objects in this manner.
To instantiate a reference object in C#, you have to use new keyword. In C# we create a reference and then point the reference at an object allocated the heap with the new keyword, like:
Object myobj;
myobj = new Object();
The above code makes a reference and instantiates myobj in Heap. The reference types with their CTS and description are listed below:
Now that we have learned the basic concepts of both the types, we will create a program in which we will see these both data types:
So in the above program you have seen that because structs are the value types and they store the data rather than their reference, no effect took place in the Value types. On the other hand the reference types modifies and shows the modified values as they are showing data from reference.
I hope you understand how to use to data types in C#. In this tutorial we have learned about the basic data types in C# and CTS, the next articles will focus on data type Conversions.
[后记]:总算把一篇文章翻译完了。翻译的过程真的让我深刻认识到“不是金刚钻,别揽瓷器活”就是真理。太痛苦了,每句话乃至每个词都要去斟酌是否有误。即使我这样做了,翻译出来的文字还是让我很不满意。所以,我想说,不用全文翻译工具的而具有技术涵养的翻译者们,你们辛苦了!
PS:短期内都不会再翻译什么文章了,一句话,技术功底还是太薄弱了。
分享到:
相关推荐
( Nutch,第1部分:爬行(译文) ( Nutch,第1部分:爬行(译文)
悟真篇全卷译文.doc的内容可以分为两个部分:第一部分是序言和自序,伯端在这里介绍了悟真篇的宗旨和目的,强调了人生的短暂和不确定性,指出只有通过修行和自省,才能达到悟真和成圣的目标。第二部分是正文,伯端在...
《Face Alignment by Explicit Shape Regression》是一篇关于面部对齐技术的学术论文,它在计算机视觉领域具有重要价值。这篇论文的核心在于提出了一种名为“显式形状回归”的方法,用于解决人脸图像中的关键点定位...
- **重置命令**:在上电后,必须将重置命令(FFh)作为第一条命令发出。 - **操作状态字节**:提供了一种软件方法来检测操作完成情况、通过/失败条件以及写保护状态。 - **数据选通信号**:DQS信号提供了一种硬件方法...
ICL7660_Datasheet汉语译文.doc
在"apachecn-bigdata-zh-master"目录下,我们可以预见到一系列与大数据相关的项目和文档,这些项目通常包括但不限于以下几个方面: 1. **Hadoop**:Hadoop是大数据处理的基础框架,提供分布式存储(HDFS)和分布式...
王力【古代汉语】第一册译文.doc
《中庸》全文及译文.pdf 《中庸》是一部中国古典哲学著作,作者是孔子弟子子思。该书主要探讨了中庸之道的内涵和实践方法。中庸之道被认为是儒家哲学的核心思想,强调人性和自然的和谐发展。 第一章 《中庸》第一...
·本文档为双语同时展示,一行原文、一行译文,可逐行对照,避免了原文/译文来回切换的麻烦; ·有原文可参照,不再担心翻译偏差误导; ·边学技术、边学英语。 ·只翻译了该翻译的内容,如:注释、说明、描述、...
在《罗织经》的第一篇中,作者讨论了人性的多样性和欺骗的本质。作者指出,人性的多样性是由于欺骗和伪装的结果,人们总是伪装自己的情感和意图。孔子也认为这种态度是可耻的。作者还强调,人的欲望多多,本性...
标签“毕业 论文 译文”表明这是一篇关于毕业论文内容的翻译,主要关注的是技术性的内容,即数据库引擎的选择,这对于计算机科学或信息技术专业的学生来说,可能是他们毕业设计或研究的一部分。 部分内容中,文章...
books_[译]_ApacheCN_大数据译文集_apachecn-bigdata-zh
《智囊全集》中的每一篇故事都是一次智慧的碰撞,通过这些故事,我们可以学习到诸如“以柔克刚”、“顺势而为”、“知己知彼”等经典战略思想。例如,"空城计"的故事,展示了诸葛亮如何利用心理战术,以空城迷惑敌人...
·本文档为双语同时展示,一行原文、一行译文,可逐行对照,避免了原文/译文来回切换的麻烦; ·有原文可参照,不再担心翻译偏差误导; ·边学技术、边学英语。 ·只翻译了该翻译的内容,如:注释、说明、描述、...
如果你是wxPython初学者,你一定会想从第一部分开始。第一章至第三章帮助您夯实 wxPython相关概念的坚实基础。第六章则对构建合理大小程序的步骤进行了完整回顾。第五章介绍如何让代码更易于管理的方法,第四章提供...
**标题与描述解读**:“新东方英文天天背80篇(有译文)”这一标题指出这是一系列适合日常背诵的英语材料,并且提供了对应的中文翻译。“有译文的天天背诵资料,天天背诵很容易的,哈哈,希望对你有用。”描述部分...
译文:C#中使用Bogus创建模拟数据 译者: Lamond Lu 背景 在我每次写技术类博文的时候,经常做的一件事就是创建模拟数据。在每篇博文中,为了解释某些概念,我需要创建许多模拟数据。这是一个我在实际中遇到的问题...
“国际标准 IEC 62055-41译文” 本资源摘要信息是关于国际标准 IEC 62055-41译文的详细介绍。该标准是电力行业中的一个重要标准,规定了付费系统中的标准传输规格和协议。 目录前言 IEC 62055-41标准的目录...
计算机英语(第三版) 刘艺 王春生主编 参考译文…… 课文A:计算机概览 一、引言 计算机是一种电子设备,它能接收一套指令或一个程序,然后通过对数字数据进行运算或对其他形式的信息进行处理来执行该程序。 要...