Lesson 1: Using Value Types第一课值类型的使用

The simplest types in the .NET Framework, primarily numeric and Boolean types, are value types.

值类型是.net框架中最简单的类型,主要包括数字和boolean类型.

Value types are variables that contain their data directly instead of containing a reference to the data stored elsewhere in memory.

值类型是变量,它包含的数据直接转换为一个在内存中数据存储位置的引用.

Instances of value types are stored in an area of memory called the stack, where the runtime can create, read, update, and remove them quickly with minimal overhead.

通过调用堆栈把值类型的实例存储在一个内存区域中, 用最小的代价快速的在运行时建立,读取,更新和移除它们.

 

More Info—Reference types 

For more information about reference types, refer to Lesson 2.

There are three general value types:三种常见的值类型

·         Built-in types默认类型

·         User-defined types用户自定义类型(结构structs)

·         Enumerations枚举类型

Each of these types is derived from the System.Value base type. The following sections show how to use these different types.

每种类型都来源于System.Value基本类型.下面的部分显示如何使用这些不同的类型.

After this lesson, you will be able to: 课后你将能够

·         Choose the most efficient built-in value type选择最有效的默认类型

·         Declare value types定义值类型

·         Create your own types建立自己的类型

·         Use enumerations使用枚举类型

Estimated lesson time: 30 minutes 课程时间30分钟

 

Built-in Value Types默认值类型

Built-in types are base types provided with the .NET Framework, with which other types are built. All built-in numeric types are value types.

默认类型是.net框架提供的基本类型,而其它几个类型是需要建立的.所有默认数字类型都是值类型.

 You choose a numeric type based on the size of the values you expect to work with and the level of precision you require.

根据你需要的精度水平和工作中预期的值的大小选择一个数字类型.

 Table 1-1 lists the most common numeric types by size, from smallest to largest. The first six types are used for whole number values and the last three represent real numbers in order of increasing precision.

1-1列出了大量公共的数字类型,从小到大的范围.前六个类型用于所有的数值,而最后三种真实数字的表达是为了在需要更高的精度时使用.

Table 1-1: Built-in Value Types

Type (Visual Basic/C# alias)

Bytes

Range

Use for

System.SByte (SByte/sbyte)

1

-128 to 127

Signed byte values

带符号byte

System.Byte (Byte/byte)

1

0 to 255

Unsigned bytes

无符号byte

System.Int16 (Short/short)

2

-32768 to 32767

Interoperation and other specialized uses

System.Int32 (Integer/int)

4

-2147483648 to 2147483647

Whole numbers and counters

System.UInt32 (UInteger/uint)

4

0 to 4294967295

Positive whole numbers and counters

System.Int64 (Long/long)

8

-9223372036854775808 to 9223372036854775807

Large whole numbers

System.Single (Single/float)

4

-3.402823E+38 to 3.402823E+38

Floating point numbers

System.Double (Double/double)

8

-1.79769313486232E+308 to 1.79769313486232E+308

Precise or large floating point numbers

System.Decimal (Decimal/decimal)

16

-79228162514264337593543950335 to 79228162514264337593543950335

Financial and scientific calculations requiring great precision

 

 

Best Practices—Optimizing performance with built-in types 

最优方法:默认类型性能优化

Int32uint32是最适合运行时执行的,所以在计算和其它经常访问数字变量的时候使用它们.而浮点运算和双精度类型是最有效率的类型.因为这些类型适合于使用硬件进行操作.

The runtime optimizes the performance of 32-bit integer types (Int32 and UInt32), so use those types for counters and other frequently accessed integral variables. For floating-point operations, Double is the most efficient type because those operations are optimized by hardware.

These numeric types are used so frequently that Visual Basic and C# define aliases for them. Using the alias is equivalent to using the full type name, so most programmers use the shorter aliases. In addition to the numeric types, the non-numeric data types listed in Table 1-2 are also value types.

这些数字类型通过在Visual Basic and C#中定义别名的方式非常频繁的被使用.

引用这个别名就相当于引用所有类型名称.所以很多程序员使用简短的别名.另外对于数字类型来说,table1-2中的非数字数据类型也都是值类型.

Table 1-2: Other Value Types

Type (Visual Basic/C# alias)

Bytes

Range

Use for

System.Char (Char/char)

2

N/A

Single Unicode characters

System.Boolean (Boolean/bool)

4

N/A

True/False values

System.IntPtr (none)

Platform-dependent

N/A

Pointer to a memory address

System.DateTime (Date/date)

8

1/1/0001 12:00:00 AM to 12/31/9999 11:59:59 PM

Moments in time

There are nearly 300 more value types in the Framework, but the types shown here cover most needs.

.net框架中有接近300多种的值类型,而在这里只包含了最需要的信息.

When you assign between value-type variables, the data is copied from one variable to the other and stored in two different locations on the stack.

当你在值类型之间进行赋值时,数据是从一个变量拷贝到另一个,并且存储在堆栈中的两个不同的位置.

This behavior is different from that of reference types, which are discussed in Lesson 2.

这种行为和引用类型的不同将会在第二课讨论.

Even though value types often represent simple values, they still function as objects.

虽然值类型经常表达简单的值,但它们的作用和对象是一样的.

In other words, you can call methods on them. In fact, it is common to use the ToString method when displaying values as text. ToString is overridden from the fundamental System.Object type.

换句话说,你可以调用它们中的方法,实际上,在把值显示为文本的时候,会用到公共的ToString()方法.ToString是覆盖了最基本的System.Object类型的方法.

 

The Object base class 

In the .NET Framework, all types are derived from System.Object. That relationship helps establish the common type system used throughout the Framework.

.net框架中,所有类型都来源于System.Object.这种帮助关系使公共类型系统得以贯穿整个.net框架