作者:不及格的程序员-八神
不要以为只有在构造函数中它们是可以更改的,其实还有其它的状况。
运行一下下面的代码你就无语了,介绍了两种方式,out输出与联合体。
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Runtime.InteropServices;
8
9 namespace Hello_2010
10 {
11 public partial class _Default : System.Web.UI.Page
12 {
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 S3 s3 = new S3();
16 int x = s3.A.X;
17 s3.B.X++; //A.X已经变成1了,不信你看看。。。。
18
19 S s = new S(42);
20 s.MultiplyInto(10, out s); //变量X变成420了,而不是42。
21
22
23 }
24
25 protected void Button1_Click(object sender, EventArgs e)
26 {
27 //this.Label1.Text = "ok";
28 //this.Label1.Text += this.FileUpload1.FileName;
29 }
30 }
31
32 struct S
33 {
34 public readonly int X;
35
36 public S(int x) { X = x; }
37
38 public void MultiplyInto(int c, out S target)
39 {
40 System.Console.WriteLine(this.X);
41 target = new S(X * c);
42 System.Console.WriteLine(this.X); // same? it is, after all, readonly.
43 }
44 }
45
46
47 struct S1
48 {
49 public readonly int X;
50 }
51
52 struct S2
53 {
54 public int X;
55 }
56
57 [StructLayout(LayoutKind.Explicit)]
58 struct S3
59 {
60 [FieldOffset(0)]
61 public S1 A;
62 [FieldOffset(0)]
63 public S2 B;
64 }
65 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Runtime.InteropServices;
8
9 namespace Hello_2010
10 {
11 public partial class _Default : System.Web.UI.Page
12 {
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 S3 s3 = new S3();
16 int x = s3.A.X;
17 s3.B.X++; //A.X已经变成1了,不信你看看。。。。
18
19 S s = new S(42);
20 s.MultiplyInto(10, out s); //变量X变成420了,而不是42。
21
22
23 }
24
25 protected void Button1_Click(object sender, EventArgs e)
26 {
27 //this.Label1.Text = "ok";
28 //this.Label1.Text += this.FileUpload1.FileName;
29 }
30 }
31
32 struct S
33 {
34 public readonly int X;
35
36 public S(int x) { X = x; }
37
38 public void MultiplyInto(int c, out S target)
39 {
40 System.Console.WriteLine(this.X);
41 target = new S(X * c);
42 System.Console.WriteLine(this.X); // same? it is, after all, readonly.
43 }
44 }
45
46
47 struct S1
48 {
49 public readonly int X;
50 }
51
52 struct S2
53 {
54 public int X;
55 }
56
57 [StructLayout(LayoutKind.Explicit)]
58 struct S3
59 {
60 [FieldOffset(0)]
61 public S1 A;
62 [FieldOffset(0)]
63 public S2 B;
64 }
65 }
南来地,北往的,上班的,下岗的,走过路过不要错过!
======================个性签名=====================
之前认为Apple 的iOS 设计的要比 Android 稳定,我错了吗?
下载的许多客户端程序/游戏程序,经常会Crash,是程序写的不好(内存泄漏?刚启动也会吗?)还是iOS本身的不稳定!!!
如果在Android手机中可以简单联接到ddms,就可以查看系统log,很容易看到程序为什么出错,在iPhone中如何得知呢?试试Organizer吧,分析一下Device logs,也许有用.