• 结构体


    结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样。

    一、定义的方法

    struct student
    
    {
    
    public int nianling;
    
    public int fenshu;
    
    public string name;
    
    public string sex;
    
    public int sum;
    
    }
    

      以上的语句就是定义一个名称为student的结构体,其中包含int类型的年龄、分数、总和,和string类型的姓名、性别。

    二、用法:

          在main主函数外面定义了一个名称为student的结构体,以便于main函数之中使用。

          student st = new student();//这句话是在main函数之中定义了一个名为st的student类型的结构体。

          下面开始为里面的每个元素赋值:(结构体名+点+结构体里面的变量名称=值)

    student st = new student()
    {
    
    st.nianling=22;
    
    st.fenshu=80;
    
    st.name="小李";
    
    }
    

      

    三、结构体类型里面包含结构体类型:

          可以在此前的student的结构体中在定义一个结构体

    struct student
    
    {
    
    public int nianling;
    
    public int fenshu;
    
    public string name;
    
    public string sex;
    
    public score fen;
    
    }
    struct score
    {
    public double yuwen;
    public double shuxue;
    public doubule yingyu;
    }
    

      score就被包含在student这个类之中。

    四 结构体放入集合中

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        class Program
        {
          struct student
    
          {
          public int nianling;
          public int fenshu;
         public string name; 
          }
    
    
    
            static void Main(string[] args)
            {
               student st = new student()
               st.nianling=22;
               st.fenshu=80;
               st.name="小李";
               arraylist al=new arrayliat();
               al.add(st);
    
    
            }
        }
    }
    

    五、接受struct内的内容

    如上面例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        class Program
        {
          struct student
    
          {
          public int nianling;
          public int fenshu;
         public string name; 
          }
    
    
    //把结构体放入集合
            static void Main(string[] args)
            {
               student st = new student()
               st.nianling=22;
               st.fenshu=80;
               st.name="小李";
               arraylist al=new arrayliat();
               al.add(st);
    
    //接收
    student st1=new stdent();
    st1=(student)al[3];
    console.writeline(st1.nianling);
    console.writeline(st1.fenshu);
    console.writeline(st1.name);
    console.readline();
    
    
    
            }
        }
    }


    完!!!

  • 相关阅读:
    MySQL
    Date和String转换
    Spring-test事务自动回滚
    java多线程:生产者和消费者模式(wait-notify) : 单生产和单消费
    制作ISO文件 与 提取ISO文件
    windows10 更换密码
    win 添加网络位置共享 && win 实用快捷键
    ubuntu16 安装mysql5.7
    ubuntu16 中chkconfig 命令不能使用
    mysql 5.7 初始化密码或随机密码
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/5735251.html
Copyright © 2020-2023  润新知