现在的位置: 首页 > 综合 > 正文

结构

2013年06月19日 ⁄ 综合 ⁄ 共 1905字 ⁄ 字号 评论关闭

       结构类型是一种复合数据类型,用于将多个不同类型的数据成员组合成一种新的类型。结构使用关键字struct声明,其中可以包含0个或任意多个成员的定义。

 

  struct Contact

  {

         public string _name;

         public int _age;

         public string _telephone;

         public string _address;

  }

 

  Contact c1;或 Contact c1 = new Contact();

  c1._name

 

    对结构成员的访问通过圆点连接符“.”进行,即结构变量 + .+ 成员变量。

    结构类型包含的成员类型没有限制,可以是简单值类型,也可以是结构类型和枚举类型,还可以是各种引用类型。

 

代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace StructExample
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Contact c1;
13             c1.name = "Messi";
14             c1.age = 21;
15             c1.telephone = "010-28493465";
16             c1.address.city = "beijing";
17             c1.address.street = "changan road";
18             c1.address.number = 256;
19 
20             Console.WriteLine(c1.name);
21             Console.WriteLine(c1.age);
22             Console.WriteLine(c1.telephone);
23             Console.WriteLine(c1.address.city);
24             Console.WriteLine(c1.address.street);
25             Console.WriteLine(c1.address.number);
26         }
27 
28         //struct Contact
29         //{
30         //    public string name;
31         //    public int age;
32         //    public string telephone;
33 
34         //    public struct Address
35         //    {
36         //        public string city;
37         //        public string street;
38         //        public int number;
39         //    }
40 
41         //    public Address address;
42         //}
43 
44         struct Contact
45         {
46             public string name;
47             public int age;
48             public string telephone;
49 
50             public Address address;
51         }
52 
53         struct Address
54         {
55             public string city;
56             public string street;
57             public int number;
58         }
59     }
60 }
61 

 执行结果:

 

抱歉!评论已关闭.