结构体定义语法:
访问修饰符 struct 结构体名
{
字段1; //字段: 访问修饰符 类型 变量名
字段2;
字段3;
...
}
定义域:(命名空间内 类外) or (类内 方法外)
结构体应用:
1.传统赋值:
static void Main(string[] args)
{
string name1 = "张三";
int age1 = 18;
string name2 = "李四";
int age2 = 14;
string name3 = "王五";
int age3 = 17;
}
2.使用结构体赋值:变量定义一次,即可多次使用
public struct Student
{
public string _name; //字段命名规则一般前面+ '_',不强制
public int _age;
}
static void Main(string[] args)
{
Student s; //先初始化结构体
s._name = "张三";
s._age = 18;
Student s1;
s1._name = "李四";
s1._age = 14;
Student s2;
s2._name = "王五";
s2._age = 17;
}