Chapter1 Framework Fundamentals 框架的基本原理

Lesson1 Using Value Types ---- 值类型的使用

主要内容:
a.基本的内建值类型
b.如何声明
c.如何自定义值类型

以下为一个Struct类型声明的例子:

// C#
struct Cycle
{
    // Private fields
    int _val, _min, _max;

    // Constructor
    public Cycle(int min, int max)
    {
        _val = min;
        _min = min;
        _max = max;
    }

    public int Value
    {
        get { return _val; }
        set
        {
            if (value > _max)
                _val = _min;
            else
            {
                if (value < _min)
                    _val = _max;
                else
                    _val = value;
            }
        }
    }

    public override string ToString()
    {
        return Value.ToString();
    }

    public int ToInteger()
    {
        return Value;
    }

    // Operators (new in .NET 2.0)
    public static Cycle operator +(Cycle arg1, int arg2)
    {
        arg1.Value += arg2;
        return arg1;
    }

    public static Cycle operator -(Cycle arg1, int arg2)
    {
        arg1.Value -= arg2;
        return arg1;
    }
}

 

以下是一个枚举类型的声明及调用例子:

// C#
enum Titles : int { Mr, Ms, Mrs, Dr };

 

// C#
Titles t = Titles.Dr;
Console.WriteLine("{0}.", t); // Displays "Dr."

总结:主要是以一个Person的Struct类型作例子讲述Struct及Enum的声明以引申至其他应用。

练习题:

1.

Which of the following are value types? (Choose all that apply.)

  1. Decimal

  2. String

  3. System.Drawing.Point

  4. Integer

answer:1,3,4

2.

You pass a value-type variable into a procedure as an argument. The procedure changes the variable; however, when the procedure returns, the variable has not changed. What happened? (Choose one.)

  1. The variable was not initialized before it was passed in.

  2. Passing a value type into a procedure creates a copy of the data.

  3. The variable was redeclared within the procedure level.

  4. The procedure handled the variable as a reference.

answer:2

3.

Which is the correct declaration for a nullable integer?

  1. ' VB
    Dim i As Nullable<Of Integer> = Nothing
    
    // C#
    Nullable(int) i = null;
  2. ' VB
    Dim i As Nullable(Of Integer) = Nothing
    
    // C#
    Nullable<int> i = null;
    
  3. ' VB
    Dim i As Integer = Nothing
    
    // C#
    int i = null;
  4. ' VB
    Dim i As Integer(Nullable) = Nothing
    
    // C#
    int<Nullable> i = null;

answer:2
点评:1.3中值类型是不能赋NULL值的,2.0中微软提供了Nullable类型,实际上这是一个结构体,以以下方式声明:
         int myNullableInt = 1;      //此值为正常声明。
         int? myNullableInt = 1;       //此值可以为NULL。
         另外还可以以泛型定义的形式声明:
         Nullable<int> i = null;
         Nullable<int> i = new Nullable<int>(3);      //以构造函数定义。

4.

You need to create a simple class or structure that contains only value types. You must create the class or structure so that it runs as efficiently as possible. You must be able to pass the class or structure to a procedure without concern that the procedure will modify it. Which of the following should you create?

  1. A reference class

  2. A reference structure

  3. A value class

  4. A value structure

answer:4

 

参考答案及解说:

1.

Correct Answers: A, C, and D

  1. Correct: Decimal is a value type.

  2. Incorrect: String is a reference type.

  3. Correct: System.Drawing.Point is a value type.

  4. Correct: Integer is a value type.

2.

Correct Answer: B

  1. Incorrect: First, value types must be initialized before being passed to a procedure unless they are specifically declared as nullable. Second, passing a null value would not affect the behavior of a value type.

  2. Correct: Procedures work with a copy of variables when you pass a value type. Therefore, any modifications that were made to the copy would not affect the original value.

  3. Incorrect: The variable might have been redeclared, but it would not affect the value of the variable.

  4. Incorrect: If the variable had been a reference, the original value would have been modified.

3.

Correct Answer: B

  1. Incorrect: The Visual Basic sample uses angle brackets rather than parentheses. The C# sample uses parentheses rather than angle brackets.

  2. Correct: This is the proper way to declare and assign a nullable integer. In C#, you could also use the following: int? i = null;

  3. Incorrect: You must use the Nullable generic to declare an integer as nullable. By default, integers are not nullable.

  4. Incorrect: This is not the correct syntax for using the Nullable generic.

4.

Correct Answer: D

  1. Incorrect: You could create a reference class; however, it could be modified when passed to a procedure.

  2. Incorrect: You cannot create a reference structure.

  3. Incorrect: You could create a value class; however, structures tend to be more efficient.

  4. Correct: Value structures are typically the most efficient.

参考资料:
C# 2.0 - Nullable
在C#中使用Nullable类型
值类型和引用类型的区别

posted on 2009-07-19 01:38  小小痕  阅读(241)  评论(0编辑  收藏  举报

导航