When is the readonly field not readonly? As you can imagine !!!

Source: Internet
Author: User
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 }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.