2.6 structure declaration and initialization

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

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.