Author: failingProgramMember-eight gods
Do not think that they can be changed only in constructors. In fact, there are other situations.
Run the following commands:CodeYou are speechless. We introduce two methods: Out output and combination.
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 has changed to 1. Don't believe it ....
18
19 S = New S ( 42 );
20 S. multiplyinto ( 10 , Out S); // variable X is changed to 420 instead of 42.
21
22
23 }
24
25 Protected Void Button#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;
62 [Fieldoffset ( 0 )]
63 Public S2 B;
64 }
65 }