zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#学习-结构体(Struct+和Class的区别,ref关键字,析构函数)

c#学习 函数 区别 结构 关键字 Class ref
2023-09-27 14:27:29 时间

相同点:

  1. 都包含字段,属性,方法
  2. 修饰的访问权限
  3. 通过new实例化对象

不同点:

  1. Struct结构体在栈上开辟空间,是值类型,不传地址,原始数据不变
  2. Class类在堆上开辟空间,是引用类型,传地址,原始数据会发生改变,可使用ref进行传地址做出值的改变
  3. 结构体中不允许写析构函数
  4. 结构体只有一个父类Object,无继承

代码:

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _TBD_2020814Test
{
    public struct Point
    {
        //字段
        public double x;
        public double y;
        //属性
        public double X   //属性访问器
        {
            set
            {
                this.x = value;
            }
            get
            {
                return x;
            }
        }
        //方法
        public void Test()
        {
        }
        //public  Point() { }结构体不允许无参构造
        public Point(double x,double y)
        {
            this.x = x;
            this.y = y;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个结构体Point对象
            Point p = new Point();//没有无参构造,但没有出问题,说明Struct中始终有一个public的无参构造方法
            Point p1 = new Point();
            p.x = 10;
            p.y = 20;
            p1.x = 10;
            p1.y = 20;
            Change(p);
            Console.WriteLine(p.x);//10
            Console.WriteLine(p.y);//20
            Console.WriteLine("不改变数值");
            Change(ref p1);
            Console.WriteLine(p1.x);//100
            Console.WriteLine(p1.y);//200
            Console.WriteLine("传地址改变数值");
            //struct是栈上开辟空间是值类型,不传地址,所以不改变值的大小
            //把Struct改成class,调用方法输出的是100,200,因为class是堆上开辟空间是引用类型,传地址,会改变地址中的值
        }
        //~point() { }没有析构函数
        public static void Change(Point p) //ref关键字传地址改变值
         {
            p.x = 100;
            p.y = 200;
        }
        public static void Change(ref Point p1) //ref关键字传地址改变值
        {
            p1.x = 100;
            p1.y = 200;
        }
    }
}

析构函数:

参考:

代码理解:

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _TBD_2020814Test
{
    class First                     // 基类First
    {
        ~First()                    // 析构函数
        {
            Console.WriteLine("~First()析构函数");
        }
    }
    class Second : First            // Second类从First类派生
    {
        ~Second()                   // 析构函数
        {
            Console.WriteLine("~Second()析构函数");
        }
    }
    class Third : Second            // Third类从Second类派生
    {
        ~Third()                    // 析构函数
        {
            Console.WriteLine("~Third()析构函数");
        }
    }
    class Program
    {
       
           
            static void Main(string[] args)
            {
                // C#析构函数
                Third Third1 = new Third(); // 创建类的实例
            }
    
    }
}