C#数据类型之结构体介绍

一、结构体定义

       在 C# 中,结构体是值类型数据结构。它使得一个单一变量可以存储各种数据类型的相关数据。struct 关键字用于创建结构体,是用来代表一个记录。结构体主要是记录存储数据属性,例如学生档案信息、图书资料编码、人物资料、三维点的信息等等,包含实例的属性。

public struct student
    { 
        //字段、属性、方法、事件
    }

二、成员类型

定义一个学生档案的结构体,里面包含学生姓名、年龄、体重等信息,结构体定义如下:

    public struct student
    {
        public int age;
        public string name;
        private double weight;      
        public double WG
        {
            set
            {
                weight = value;
            }
            get
            {
                return weight;
            }

        }
        public void show()//显示学生的个人能力、学习能力等展示信息
        { 
                
        }
    }

1、字段和属性

注意:

1.在结构体中字段只能够通过构造函数来实现初始化(除非字段被const 或者是 static 修饰),不像在类中我们可以在创建字段的时候直接对齐初始化。未被赋值的字段和属性如果是int类型默认值为0,字符串的默认值为空。

在结构体中字段和属性是不能进行初始化赋值的,只有通过const设置为常量,或者static设置为静态变量即可作为结构体通用数据赋予初始值。其中static可以在主函数内修改该数据,而const定义的变量无法被修改。

值类型的默认值如下图:

定义结构体:

        public static string school = "清华";
        public const int school_level = 1;
        public int age;
        public string name;
        private double weight;   //属性的私有类型可以保护数据
        public double WG //为属性
        {
            set
            {
                weight = value;
            }
            get
            {
                return weight;
            }
        }

主函数:

            student stu1 = new student();
            Console.WriteLine("统计学校: "+student.school);
            Console.WriteLine("学校的等级: "+student.school_level);
            Console.WriteLine("学生的年龄:"+stu1.age);
            Console.WriteLine("学生的姓名:" + stu1.name);
            Console.WriteLine("学生的体重:"+stu1.WG);
            Console.ReadKey();

 输出结果:

2.初始化只能通过两种形式第一种是通过带参的构造函数,第二种是通过实例化后赋值;

构造函数内一定要对字段和属性进行初始化 ,如果定义了事件,对事件也要进行初始化。

  第一种方法:构造函数初始化,结构体中构造函数如下

public student(int age,string name,double weight)
{
    this.age = age;
    this.name = name;
    this.weight = weight;
  
}

主函数

            student stu1 = new student(15,"xiaoming",47.8);
            student.school = "北大";
            Console.WriteLine("统计学校: "+student.school);
            Console.WriteLine("学校的等级: "+student.school_level);
            Console.WriteLine("学生的年龄:"+stu1.age);
            Console.WriteLine("学生的姓名:" + stu1.name);
            Console.WriteLine("学生的体重:"+stu1.WG);

输出结果为:

 第二种方法:在主函数中直接对结构体数值进行初始化;

            student stu2 = new student();
            stu2.age = 16;
            stu2.name = "xiaogang";
            stu2.WG = 53.14;
            Console.WriteLine("学生的年龄:" + stu2.age);
            Console.WriteLine("学生的姓名:" + stu2.name);
            Console.WriteLine("学生的体重:" + stu2.WG);

输出结果为:

2.方法

结构体是值类型数据,当一个结构体变量作为函数参数传递时, 属于值类型的值传递,也就是传入的是一个副本,函数体内对结构体的改变,不会影响方法体外的结构体变量。如果想要改变,可以使用ref 或者out 进行值类型的引用传递,这样,传入的参数就是结构体本身,当方法体内改变了结构体变量也就是改变了方法体外的结构体变量。
 

结构体内定义方法:

        public void Addweight(ref student student)
        {
            student.weight += weight_inhense;
            this.weight = 38.5;
        }

输出结果为:

3.事件

结构体内可以定义委托,事件为委托的实例化方法的载体。

定义委托和事件

        public delegate void publisher(student student);
        public event publisher Receive;

如果结构体内有构造函数。需要对事件也进行初始化。

调用事件

        public void show()//显示学生的个人能力、学习能力等展示信息
        {
            Receive(this);
        }

主函数内塞入事件的方法:

    public static class Stu_asker
    {
        public static void Study(student student)
        {
            Console.WriteLine("此学生的学习能力为: "+ student.school_level*10);
        }

        public static void happy(student student)
        {
            Console.WriteLine("此学生的开心指数为: " + (student.school_level+10));
        }
    }

 


            stu1.Receive += Stu_asker.Study;
            stu1.Receive += Stu_asker.happy;
            stu1.show();

三、类和结构体的区别

  1、结构是值类型,它在栈中分配空间;而类是引用类型,它在堆中分配空间,栈中保存的只是引用。

  2、结构类型直接存储成员数据,让其他类的数据位于对中,位于栈中的变量保存的是指向堆中数据对象的引用。

  3、结构类型可以有实例构造函数和静态构造函数,但不能有析构函数。

主要参考以下文章:
c# --- 结构体(struct)_c# 结构体_yang28242687的博客-CSDN博客

四、整体程序

using System;
namespace test
{
    public class qiao
    {

        public delegate void abb(int test);
        static void Main(string[] args)
        {

            
            student stu1 = new student(15,"xiaoming",47.8);
            student.school = "北大";

            student stu2 = new student();
            stu2.age = 16;
            stu2.name = "xiaogang";
            stu2.WG = 53.14;
            stu1.Addweight(ref stu2);

            Console.WriteLine("学生的年龄:" + stu2.age);
            Console.WriteLine("学生的姓名:" + stu2.name);
            Console.WriteLine("学生的体重:" + stu2.WG);

            Console.WriteLine("统计学校: " + student.school);
            Console.WriteLine("学校的等级: " + student.school_level);
            Console.WriteLine("学生的年龄:" + stu1.age);
            Console.WriteLine("学生的姓名:" + stu1.name);
            Console.WriteLine("学生的体重:" + stu1.WG);


            stu1.Receive += Stu_asker.Study;
            stu1.Receive += Stu_asker.happy;
            stu1.show();

            Console.ReadKey();
        }

    }




    public struct student
    {

        public delegate void publisher(student student);
        public event publisher Receive;

        public static string school = "清华";
        public const int weight_inhense = 10;
        public const int school_level = 1;

        public int age;
        public string name;
        private double weight;      
        public double WG
        {
            set
            {
                weight = value;
            }
            get
            {
                return weight;
            }

        }
        public student(int age,string name,double weight)
        {
            this.age = age;
            this.name = name;
            this.weight = weight;
            this.Receive = null; //对事件也要进行初始化;
  
        }


        public void Addweight(ref student student)
        {
            student.weight += weight_inhense;
            this.weight = 38.5;
        }


        public void show()//显示学生的个人能力、学习能力等展示信息
        {
            Receive(this);
        }
    }

    public static class Stu_asker
    {
        public static void Study(student student)
        {
            Console.WriteLine("此学生的学习能力为: "+ student.school_level*10);
        }

        public static void happy(student student)
        {
            Console.WriteLine("此学生的开心指数为: " + (student.school_level+10));
        }
    }

}

输出结果为:

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值