ValueType overrides the virtual methods from Object with more appropriate (适当的)implementations(执行) for value types. See also Enum, which inherits from ValueType.
ValueType 重写从Object继承得到的虚方法,以使这些虚方法更好地配合ValueType。
Enum继承自ValueType。
————————————————————————————————
[] [(true)] [(.AutoDual)] public class Object
Object的一个成员:
ToString()————————————————————————————————————
--Returns a that represents the current .
public virtual ToString ()
1
using
System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace BoxedValueTypeField
6 {
7
8 struct Point
9 {
10 public Int32 x, y;
11 public void Change(Int32 x, Int32 y)
12 {
13 this.x = x;
14 this.y = y;
15 }
16 public override string ToString()
17 {
18 return String.Format("({0},{1})",x,y);
19 }
20 }
21 class Program
22 {
23 static void Main(string[] args)
24 {
25 Point p = new Point();
26 p.x = p.y = 1;
27 Console.WriteLine(p);
28
29 p.Change(2, 2);//调用了栈(Point)上的方法,改变了Point的字段
30 Console.WriteLine(p);//output(2,2)
31 //Console.WriteLine(Object value)
32
33 Object o = p; //p被boxed,赋给o
34 Console.WriteLine(o);//output (2,2)
35 Console.WriteLine(p);//output (2,2)
36 Console.WriteLine("---------");
37 p.Change(5, 5);
38 Console.WriteLine(p);
39 //但是这里o和p是两段内存中的两个对象,
40 //所谓boxed,其实是一个移动的过程,
41 //从栈(Point)上移动了一个p到堆(Program)上
42 //其实是"地上有一瓶酒,装到箱子里,地上的这瓶酒到箱子里去了"
43
44
45
46 //把对象o临时Unboxed到一个临时栈上,将o转型为Point会对o执行Unboxed
47 ((Point)o).Change(3, 3);
48 //调用Change方法,该临时栈中的Point的字段的值为(3,3),已装箱Point对象却不会受影响
49
50 Console.WriteLine(o);
51 //但o(在堆上)本身没有改变,所以这里输出(2,2)
52
53 //这里的p是已经boxed的p,还是栈上的p,Jeffery说是已经boxed的
54 Console.WriteLine(p);
55 //p(在栈上)也没有改变,所以也输出(2,2),--前面已调用了p.Change(2,2)
56
57 //Point tempP = ((Point)o).Change(3, 3); 要想得到这个(3,3)怎么办?
58 //Console.WriteLine(tempP);
59
60 Console.Read();
61
62 }
63 }
64}
65
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace BoxedValueTypeField
6 {
7
8 struct Point
9 {
10 public Int32 x, y;
11 public void Change(Int32 x, Int32 y)
12 {
13 this.x = x;
14 this.y = y;
15 }
16 public override string ToString()
17 {
18 return String.Format("({0},{1})",x,y);
19 }
20 }
21 class Program
22 {
23 static void Main(string[] args)
24 {
25 Point p = new Point();
26 p.x = p.y = 1;
27 Console.WriteLine(p);
28
29 p.Change(2, 2);//调用了栈(Point)上的方法,改变了Point的字段
30 Console.WriteLine(p);//output(2,2)
31 //Console.WriteLine(Object value)
32
33 Object o = p; //p被boxed,赋给o
34 Console.WriteLine(o);//output (2,2)
35 Console.WriteLine(p);//output (2,2)
36 Console.WriteLine("---------");
37 p.Change(5, 5);
38 Console.WriteLine(p);
39 //但是这里o和p是两段内存中的两个对象,
40 //所谓boxed,其实是一个移动的过程,
41 //从栈(Point)上移动了一个p到堆(Program)上
42 //其实是"地上有一瓶酒,装到箱子里,地上的这瓶酒到箱子里去了"
43
44
45
46 //把对象o临时Unboxed到一个临时栈上,将o转型为Point会对o执行Unboxed
47 ((Point)o).Change(3, 3);
48 //调用Change方法,该临时栈中的Point的字段的值为(3,3),已装箱Point对象却不会受影响
49
50 Console.WriteLine(o);
51 //但o(在堆上)本身没有改变,所以这里输出(2,2)
52
53 //这里的p是已经boxed的p,还是栈上的p,Jeffery说是已经boxed的
54 Console.WriteLine(p);
55 //p(在栈上)也没有改变,所以也输出(2,2),--前面已调用了p.Change(2,2)
56
57 //Point tempP = ((Point)o).Change(3, 3); 要想得到这个(3,3)怎么办?
58 //Console.WriteLine(tempP);
59
60 Console.Read();
61
62 }
63 }
64}
65