C# == 和 Equals 区别

只愿长相守 提交于 2020-04-05 22:59:23
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var s1 = new string("yyy");
            var s2 = new string("yyy");

            var s3 = new StringBuilder("yyy");
            var s4 = new StringBuilder("yyy");
            Console.WriteLine("object.ReferenceEquals(s1, s2): " + object.Equals(s1, s2));
            Console.WriteLine("object.ReferenceEquals(s3, s4): " + object.Equals(s3, s4));
            Console.WriteLine("s1 == s2: " + (s1.GetHashCode() + "  " + s2.GetHashCode()));
            Console.WriteLine("s1 == s2: " + (s1.GetHashCode() + " "  + s2.GetHashCode()) + " " + s1.Equals(s2));
            Console.WriteLine("s3 == s4: " + (s3.GetHashCode() + " " + s4.GetHashCode())  + " " + s3.Equals(s4));

            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            Console.ReadKey();
        }
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!