Structure (struct)

Source: Internet
Author: User
This lesson describes the structure syntax and the difference between it and the class. U/K) S
_ V) C *] (}. @
The following two examples are given. The first example describes how to use the structure. The second example describes how to transmit an instance to sgbfq6a 'B ^ 6?
Method
Di7zh9axu8s
The differences between structures and classes also involve the following concepts.
4 V/Dita ^ /{
* Structure and class
Hhvtj % ~ BK
Z
Tkp
* Heap and stack
0/8rw0 /~ Id _
* Construct and inherit! Rpioq5uc & D
* Attribute (attributes) and structure L & | # J "P. o
Example 1 me-'_-WC; @ 1i
The following example declares a structure with three members: an attribute, a method, and a private domain.
| {Xfui4ut; Z
000: // structs/struct1.cs nv1wd; V] % i0vq
001: using system;
Triel "m
002: struct simplestruct D8 ^ Cz6M-u % B
003 :{
E) S (KS
K
004: Private int xval;
# Dw1enzk 'pd
005: Public int x
% -[}
NW/Q
006 :{
'W @ k8u7b 'z J
007: Get {
& T9dnx: pw! U'
008: Return xval; (Y. v @; mm {./% [
009 :}
QX
Od, v5u "e6cd
010: set {5 @ ^
| 9y n2c
011: If (value <100) ^ vab1al: R
012: xval = value;
NWA
J
L
013 :}& ^ (lobby: Is
014:} 8 _ | + _: wbo2s]
015: Public void displayx ()
/Q'jd9f) d
016: {-US * J. H: I; A m
017: console. writeline ("the stored value is: {0}", xval); 9A on (| @ 8ahu? &
018 :}
{
F, B [EJ o
019 :}
0p7d
T4 ^ X,}/-P %? I
020:
-C7ig ^ *~ /Y3t
021: Class testclass Q
Cr (}! PIR "}-n
022: {PE) E % [0iq "x
023: public static void main ()
+ J7 _ +] V ,? Mn8ss
024 :{
^/RL
T9i ~ 4d3f {Ki
025: simplestruct Ss = new simplestruct ();-G ^ 2bw ', W
Y J! N6s
026: SS. X = 5;
, Ggtq-BCU
027: Ss. displayx ();
@ R3f & k2y1jpn
028 :}
5_t "/T # lhcf-^ 1N
029:} lo | syjw
Output result kc3h7epsz3w
U
The stored value is: 5, vvk + I $ H | 1 m/5 K
[Structure and class]
+ D % v8j3oxp
The structure can be seen as a class, but it is different from the class, the class is a reference type, and the Structure
I5? /I1d7s3z # O $ oo_a
^ J4h! Do 'R. c
Is the value type. You can also use the structure to define the type object so that it looks like the built-in type. @ T # yp
Dyn _
[Heap and stack]: rn & donr % B # VJ * k/h
When you use the new operator for the class, it allocates space from the heap. However, when you instantiate a structure, it is in the heap

Cy + r3l
'Nw '@
Stack
+ M (ED
GQ @. s '@ Zn
In this way, the performance will be different. Moreover, you cannot handle structure instance references like the operation class.
CSF (] 6i + w-U + }{
Because
/{7ljr, FN DZ
Therefore, when a structure is passed through a parameter, it is passed through a value instead of a reference., qb3w
Dtlo
Example 2 Z & day3 @
In this example, the structure is passed as a parameter, while the class is passed by reference.
: Zhl?
DF
F
000: // structs/struct2.cs
(O, BHW r9zt
001: using system;
Y8pa % Hu
002: % czfiw. {* F: W
TX
003: Class theclass u
C: qu0 _ j * F: ^
004: {"/. E & No: f}
005: Public int X;
HI7Tnu-P 8n}
006 :}
SM 'jguw Z $ y
007:. B };@
C
E # Rb
008: struct thestruct
* R5kv; njwb! S & R} NV
009: {. jj3ty7 {EZ) E
Z-J @
010: Public int X;
2L [Z-E $ o9g, Su
011:} 7 @) ey, I % a2bb C
012 :.? 'Iu '9eqm
013: Class testclass
6 njzb; f3a: gp ~
Y
014: {ef1iap $ C % jhh1g $ m
015: public static void structtaker (thestruct s) * x] 4 H * {n1wl
016: {m9z "} 9_r4yd; t
017: S. x = 5;
G #? 4O * ^ &? @ W
018:} iz; czni
U/1f]
019: public static void classtaker (theclass C)
8jq} H % '. P "c8b
020: {& L} 3tp/LD
@/TV
021: C. x = 5;
4bk3h6z-y
022 :}
% Y/4fnfj6r]
023: public static void main () "gix {: Tg
S
024: {l5j (B, ^ # Wem]
025: thestruct A = new thestruct ();
] 8xk ~ A "a3cy * WI
026: theclass B = new theclass (); 9C! D5bzb;/J
027: A. x = 1;
2jk/Z ^ TJ # bm7p
028: B. x = 1; *] (JP
Wu
029: structtaker ();
0ne | bpi4/"d
030: classtaker (B );
3gt #} t5c
031: console. writeline ("A. X = {0}", A. X); VQ. g9ctfjof-{. c
032: console. writeline ("B. x = {0}", B. X); G ~ U) J (@ o | TX
033 :}
% GH 'o # wk % BS {
034:} 8/) E (_ d) r
Output result s |! J] 1u
Y
A. x = 1
"Szgzy) y #@
B. x = 5
# C9k
~ : FT-Co (u * d
Code Discussion
& A, mmwuw
The above example shows that when the structure is passed as a parameter, a copy is generated, and the class is different, and 3i3iy 'd n inside the method & |
Pair
Di-/ np3kv
Class instance direct reference. 8j5 {7xy-s/
Construction and inheritance: l3zk
O (M
] 4xm5x
The structure can also have constructors, but the constructor must have parameters. The constructor without parameters is
B, 'n9d "H * Fe # u
Error ox4 ~ 3 ~ S
Structure members cannot be initialized. A structure constructor is usually used to initialize its members. V @; Oct
When you use the new operator to create a structure, the appropriate constructor will be called. However, unlike the class
^ 7zs0h7a % s] T
Yes, O (B. u2l ~ "Q6p4u
The structure can be initialized using the new operator. If you do not use new, the structure domain will not be initialized. 3 P ~ PW s! T]
The structure cannot be inherited or inherit other structures and classes, and it cannot be the base class of the class. However, the structure can be $ C hmu5n $] k0md
Follow
, Z + BG "} K % j0z
The following code demonstrates the implementation of a structured interface.
Lgssh7z] m
Interface iimage
8 [o $ Y _} 4rq) Zn
{@ HB "O] J $ J, c1j $ k5x
Void paint (); X & ^ 9vck /? ADU
} Tusms5my $ L
Struct picture: iimage
H {+ cyy? I * wkim
{
+ Q0n' ^ ~ 6tzr: E
Public void paint ()
: Cql (D * F $ A/VR _
{
Do5 @ 2bi & og % cs8p
// Painting code goes here
Wavevpp0rmq "G Y
} _] 5S
} XC! T
Private int x, y, z; // other struct members
D8o/{P-LZ "pf %]
}
; Z7iew1i/@'m
Attribute and structure ix4x % XG + u5w! E
The attribute can be used to determine the structure distribution in the memory. For example, you can use
V) HKC-/F
The structure of the structlayout (layoutkind. Union) attribute generates a combination similar to that in C/C ++, RH:/p r; t
Using system. runtime. interopservices;: l "J" F! | J
[Structlayout (layoutkind. Union)]

Hz .~ Bi *]
Struct testunion ij5by $ m) I $ P. G; d | I
{E! L) n? /VK-_ c
Public int I;
0b: LM */|
Public double D;
2O % HQ, kja7}
Public char C;
{'{9wz! W $ gp8d/L C
Public byte B1;
# | 5olh {o
}
9 ?, Q/^ s _ [& z9m K
All fields in testunion start from the same space address.
Xunpd1y @/n8ck
You can also use structlayout (layoutkind. explicit) and fieldoffset to specify [/Code]
Related Article

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.