zoukankan      html  css  js  c++  java
  • object.ReferenceEquals 对象的比较(判断两个对象是否相等)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Compare
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Person p1 = new Person();
                // p1.Name = "小强";
                // p1.Age = 18;
                // Person p2 = new Person();
                // p2.Name = "小强";
                // p2.Age = 18;
                // //Equals  不准确
                //// if (p1==p2)
                // if(p1.Equals(p2))
                // {
                //     Console.WriteLine("同一个对象");
                // }
                // else
                // {
                //     Console.WriteLine("不是同一个对象");
                // }
    
                string s1 = "123";
                string s2 = "123";//new string(new char[]{'1','2','3'});
                //if (s1==s2)
                //if(s1.Equals(s2))

    //只要看是否占同一内存
    if (object.ReferenceEquals(s1, s2)) { Console.WriteLine("同一个对象"); } else { Console.WriteLine("不是同一个对象"); } Console.ReadKey(); } } class Person { public string Name { get; set; } public int Age { get; set; } public override bool Equals(object obj) { Person pp = (Person)obj; if (this.Name == pp.Name && this.Age == pp.Age) { return true; } return false; } } }
  • 相关阅读:
    Git删除不存在对应远程分支的本地分支
    Git删除远程分支
    将博客搬至CSDN
    HttpStatus
    Mysql 日期
    jekyll开发静态网站
    修改maven默认的jdk版本
    使用@Value进行静态常量的值注入
    妙笔生花处,惊艳四座
    Integer 和 int 值比较
  • 原文地址:https://www.cnblogs.com/ink-heart/p/5900178.html
Copyright © 2011-2022 走看看