鸟语天空
System.Object
post by:追风剑情 2019-10-10 18:10

官方文档 https://docs.microsoft.com/zh-cn/dotnet/api/system.object?view=net-5.0

示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 5;
            int b = 5;
            double c = 5;
            object obj1 = new object();
            object obj2 = new object();
            object obj3 = obj2;

            //值相等或引用相等都返回true
            Console.WriteLine("==");
            Console.WriteLine("a==b : {0}", a==b);
            Console.WriteLine("a==c : {0}", a==c);
            Console.WriteLine("obj1==obj2 :{0}", obj1==obj2);
            Console.WriteLine("obj3==obj2 :{0}", obj3 == obj2);

            //Equals会先进行类型判断,如果类型不同,则直接返回false
            Console.WriteLine();
            Console.WriteLine("Equals");
            Console.WriteLine("Equals(a, b) : {0}", Object.Equals(a, b));
            Console.WriteLine("Equals(a, c) : {0}", Object.Equals(a, c));
            Console.WriteLine("Equals(obj1, obj2) :{0}", Object.Equals(obj1, obj2));
            Console.WriteLine("Equals(obj3, obj2) :{0}", Object.Equals(obj3, obj2));

            //ReferenceEquals仅比较引用
            Console.WriteLine();
            Console.WriteLine("ReferenceEquals");
            Console.WriteLine("ReferenceEquals(a, b) : {0}", Object.ReferenceEquals(a, b));
            Console.WriteLine("ReferenceEquals(a, c) : {0}", Object.ReferenceEquals(a, c));
            Console.WriteLine("ReferenceEquals(obj1, obj2) :{0}", Object.ReferenceEquals(obj1, obj2));
            Console.WriteLine("ReferenceEquals(obj3, obj2) :{0}", Object.ReferenceEquals(obj3, obj2));

            Console.WriteLine();
            Console.WriteLine("a HashCode: {0}", a.GetHashCode());
            Console.WriteLine("obj1 HashCode: {0}", obj1.GetHashCode());

            Console.WriteLine();
            Console.WriteLine("obj1.GetType() : {0}", obj1.GetType());
            Console.WriteLine("obj1.ToString() : {0}", obj1.ToString());

            //创建当前 System.Object 的浅表副本。
            //MemberwiseClone();

            Console.Read();
        }
    }
}

运行测试
1111.png

示例二:重新Object的方法
EqualsFinalizeGetHashCodeGetTypeMemberwiseCloneReferenceEqualsToString

using System;
using System.Diagnostics;
/// <summary>
/// Object 类
/// https://docs.microsoft.com/zh-cn/dotnet/api/system.object?view=net-5.0
/// </summary>
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p = null;
            GC.Collect();
            Console.Read();
        }
    }

    public class IdInfo
    {
        public int IdNumber;

        public IdInfo(int IdNumber)
        {
            this.IdNumber = IdNumber;
        }
    }

    public class Person : IDisposable
    {
        public int Age;
        public string Name;
        public IdInfo IdInfo;
        private Stopwatch sw;//秒表
        private bool _disposed = false;

        public Person()
        {
            sw = Stopwatch.StartNew();
            Console.WriteLine("Instantiated Person");
        }

        //浅表复制
        public Person ShallowCopy()
        {
            return (Person)this.MemberwiseClone();
        }

        //深度复制
        public Person DeepCopy()
        {
            Person other = (Person)this.MemberwiseClone();
            other.IdInfo = new IdInfo(IdInfo.IdNumber);
            other.Name = String.Copy(Name);
            return other;
        }

        //用于比较两个对象的引用是否相等
        //Object.ReferenceEquals(object? objA, object? objB)

        //用于比较两个对象的值是否相等
        //重写相等判断
        public override bool Equals(object obj)
        {
            Person p = obj as Person;
            if (p == null)
                return false;
            bool equal = Name.Equals(p.Name) &&
                Age == p.Age && 
                IdInfo.IdNumber == p.IdInfo.IdNumber;
            return equal;
        }

        //重写HashCode
        public override int GetHashCode()
        {
            return IdInfo.IdNumber;
        }

        //重写ToString
        public override string ToString()
        {
            string s = string.Format("Name={0},Age={1}",
                Name, Age);
            return s;
        }

        //采用dispose模式释放资源
        public void Dispose()
        {
            //释放所有资源
            Dispose(true);
            //如果所有资源已经在Dispose方法中释放了,
            //则调用下面的代码告诉GC不必执行析构函数了
            GC.SuppressFinalize(this);
        }

        //disposing=false:说明是来自终结器(析构函数)的调用
        //disposing=true: 说明是来自Dispose方法的调用
        protected void Dispose(bool disposing)
        {
            if (_disposed)
                return;

            //TODO:: 释放非托管资源

            if (disposing)
            {
                //TODO:: 释放托管资源
            }

            _disposed = true;

            //如果继承了基类,则还要调用下面这句代码
            //base.Dispose(disposing);
        }

        //注意:GC执行析构函数的目的就是让程序员自己去释放非托管资源
        //在析构函数中释放非托管资源,而不是重写Finalize方法
        ~Person()
        {
            //当析构函数执行时,说明托管资源已被GC释放,此时只需释放下非托管资源
            //释放非托管资源
            Dispose(false);
            Console.WriteLine("Finalizing object");
            sw.Stop();
            Console.WriteLine("This instance of {0} has been in existence for {1}",
                              this, sw.Elapsed);
        }
    }
}



评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容