今天用c#中的结构类型来展示一下信息,刚开始学还是有一丢丢的难的。我们都知道结构类型在生活中用的其实比较多的,他可以用来描述一个完整的事物。c#中定义结构类型使用struct关键词来标记,结构类型包括数据成员、成员函数等,例如下面的学习就是对学生成员的函数,具体如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//构建学生的整体框架
struct student
{
//学生的一些基本属性
public int stuNo;//学生的学号
public string name;//姓名
public decimal height;//身高
public string sex;//性别
public void Study(student t,string subject)//学生结构体的函数
{
stuNo = Convert.ToInt32(Console.ReadLine());
name = Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
student stu1;//声明结构类型
stu1.stuNo = 209010368;//给结构数据成员学生赋值
stu1.name = "高美丽";
stu1.height = 160;
stu1.sex = "女";
//读取结构类型的数据
Console.WriteLine("学生的学号:{0}\n学生的姓名:{1}\n学生的身高:{2}\n学生的性别:{3}", stu1.stuNo, stu1.name, stu1.height, stu1.sex);
}
}
}