C#之结构类型变量

设计一个控制台说明结构类型的应用:

using System;
namespace proj2
{ class program
    { struct Student  //结构类型声明
        {
            public string xm;
            public int nl;
            public int xh;
        }
        static void Main(string[] args)
        {
            Student s1, s2;    //结构类型变量的定义
            s1.xm = "貂蝉"; s1.nl = 20; s1.xh = 001;   //访问结构变量字段
            Console.WriteLine("姓名:{0},年龄:{1},学号:{2}", s1.xm, s1.nl, s1.xh);
            s2 = s1;
            s2.xm = "甄姬"; s2.xh = 002;
            Console.WriteLine("姓名:{0},年龄:{1},学号:{2}", s2.xm, s2.nl, s2.xh);
            Console.WriteLine("姓名“{0},年龄:{1},学号:{2}", s1.xm, s1.nl, s1.xh);
        }


        }

}


在执行s2=s1后并修改s2后,s1没有改变,说明s1,s2存储在不同的位置


             

猜你喜欢

转载自blog.csdn.net/qq_42200934/article/details/80386004
今日推荐