首页 > 代码库 > C#结构体
C#结构体
结构体:相当于是我们自己定义的一种复杂的类型。
int... double float bool char string DateTime 数组类型
生活中大部份的对象都是复合型的对象。
如何定义结构体类型?
一般来说结构体的定义要放在class的外面或class的里面,尽量不放在Main的里面。
struct 自定义类型名
{
public 变量类型 变量名;
......;
......;
......;
}
例如:
struct YuanGong //自定义的数据类型。用来描述员工的信息。
{
public string NO;
public string Name;
public int Age;
public string Nation;
public bool Sex;
}
如何用自定义的类型来定义变量?
自定义类型名 变量 = new 自定义类型名();
如何使用自定义类型的变量?
变量.子变量 = "xxxx";
Console.WriteLine(变量名.子变量);
例如:
//定义自定义类型的变量
YuanGong zhangsan = new YuanGong();
//给变量赋值
zhangsan.NO = "Y001";
zhangsan.Name = "张三";
zhangsan.Age = 22;
zhangsan.Sex = true;
zhangsan.Nation = "汉族";
//对变量取值
Console.WriteLine(zhangsan.NO+"\t"+zhangsan.Name+"\t"+zhangsan.Age);
Console.WriteLine(zhangsan.Nation+"\t"+(zhangsan.Sex?"男":"女"));
练习:
1.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 struct YuanGong //自定义数据类型,用来描述员工的信息。 9 {10 public string NO;11 public string Name;12 public int Age;13 public string Nation;14 public bool sex;15 }16 17 class Program18 {19 static void Main(string[] args)20 {21 //定义自定义类型的变量22 YuanGong zhangsan = new YuanGong();23 //给变量赋值24 zhangsan.NO = "Y001";25 zhangsan.Name = "张三";26 zhangsan.Age = 22;27 zhangsan.sex = true;28 zhangsan.Nation = "汉族";29 30 YuanGong lisi = new YuanGong();31 lisi.NO = "Y002";32 lisi.Name = "李四";33 lisi.Age = 25;34 lisi.sex = false;35 lisi.Nation = "回族";36 37 //给变量取值38 Console.WriteLine("********张三的个人信息********");39 Console.WriteLine(zhangsan.NO+"\t"+zhangsan.Name+"\t"+zhangsan.Nation);40 Console.WriteLine(zhangsan.Age+"\t"+(zhangsan.sex?"男":"女"));41 }42 }43 }
2.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 struct XueSheng 9 {10 public int xuehao;11 public string xingming;12 public double yuwen;13 public double shuxue;14 public double waiyu;15 public double zongfen;16 public int mingci;17 }18 class Class119 {20 21 static void Main(string[] args)22 {23 XueSheng[] a = new XueSheng[5];24 //输入25 for (int i = 0; i < a.Length;i++ )26 {27 Console.WriteLine("正在输入第"+(i+1)+"个学生的信息");28 a[i].xuehao = i + 1;29 Console.Write("姓名:");30 a[i].xingming = Console.ReadLine();31 Console.Write("语文:");32 a[i].yuwen = Convert.ToDouble(Console.ReadLine());33 Console.Write("数文:");34 a[i].shuxue = Convert.ToDouble(Console.ReadLine());35 Console.Write("外语:");36 a[i].waiyu = Convert.ToDouble(Console.ReadLine());37 38 a[i].zongfen = a[i].yuwen + a[i].shuxue + a[i].waiyu;39 Console.WriteLine("总分:"+a[i].zongfen);40 }41 //排序42 for (int i = 1; i <= a.Length - 1;i++ )43 {44 for (int j = 1; j <= a.Length - i; j++)45 {46 if (a[j].zongfen > a[j - 1].zongfen)47 {48 XueSheng temp = a[j];49 a[j] = a[j - 1];50 a[j - 1] = temp;51 }52 }53 }54 //写名次55 for(int i=0;i<a.Length;i++)56 {57 a[i].mingci = i + 1;58 }59 //输出60 for (int i = 0; i < a.Length; i++)61 {62 Console.WriteLine(a[i].xuehao+"\t"+a[i].xingming+"\t"+a[i].yuwen+"\t"+a[i].shuxue+"\t"+a[i].waiyu+"\t"+a[i].zongfen+"\t"+a[i].mingci);63 }64 }65 66 }67 }
C#结构体