• 结构


           结构类型是一种复合数据类型,用于将多个不同类型的数据成员组合成一种新的类型。结构使用关键字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 

     执行结果:

     

  • 相关阅读:
    MSScriptControl .net 后台接收并计算前端输入的计算表达式
    IIS 应用程序池 配置:(asp.net+sqlserver)
    java 技术架构 for web
    java 学习汇总
    js获取数组对象再多数组中出现次数
    vue人脸识别
    快速压缩图片方法(小白篇)
    【其它】种草,长草,狂草,拔草等中英文翻译
    类Unix系统中,fd指的啥?
    带你了解以太网
  • 原文地址:https://www.cnblogs.com/libingql/p/1690925.html
Copyright © 2020-2023  润新知