Code
1 using system;
2 using system. Collections. Generic;
3 using system. text;
4
5 namespace _. _ structdeclareandinitialize
6 {
7 class Program
8 {
9 static void main (string [] ARGs)
10 {
11 mypen pen1 = new mypen (1, 1); // create a schema instance using the constructor
12 mypen pen2 = pen1; // use the operator "=" to assign a value.
13 mypen pen3; // directly declare the structure variable without using the constructor
14 pen2.color = 2;
15 pen3.color = 1; // The value of the pen3 member is the same as that of the pen1 member.
16 pen3.size = 1;
17 console. writeline ("pen1 data: Color {0} size {1}", pen1.color, pen1.size );
18 console. writeline ("pen2 data: Color {0} size {1}", pen2.color, pen2.size );
19 console. writeline ("pen3 data: Color {0} size {1}", pen3.color, pen3.size );
20 console. writeline ("whether pen3 is equal to pen1: {0}", pen3.equals (pen1 ));
21 console. Readline ();
22}
23
24 struct mypen
25 {
26 Public int color;
27 public int size;
28
29 public mypen (INT icolor, int isize) // The constructor can be declared in the structure, but cannot be the default one. constructors without Parameters
30 {
31 color = icolor;
32 size = isize;
33
34}
35}
36
37}
38}
39
40
K