System.Object

作者:追风剑情 发布于:2019-10-10 18:10 分类:C#

示例

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

标签: C#

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号