如何在 LINQ C# 编程中同时使用 Take 和 Skip 运算符
我们创建了 Employee 类的两个实例 e 和 e1。将 e 赋值给 e1。这两个对象都指向同一引用,因此对于所有 Equals,我们都会获得 true 作为预期的输出。
在第二种情况下,我们可以观察到,即使属性值是相同的。Equals 返回 false。本质上,当参数引用不同的对象时。Equals 不会检查值,并且始终返回 false。
示例 1
class Program{ static void Main(string[] args){ Employee e = new Employee(); e.Name = "Test"; e.Age = 27; Employee e2 = new Employee(); e2 = e; var valueEqual = e.Equals(e2); Console.WriteLine(valueEqual); //2nd Case Employee e1 = new Employee(); e1.Name = "Test"; e1.Age = 27; var valueEqual1 = e.Equals(e1); Console.WriteLine(valueEqual1); Console.ReadLine(); } } class Employee{ public int Age { get; set; } public string Name { get; set; } }
输出
True False
示例 2
class Program{ static void Main(string[] args){ Employee e = new Employee(); e.Name = "Test"; e.Age = 27; Employee e2 = new Employee(); e2 = e; var valueEqual = e.Equals(e2); Console.WriteLine(valueEqual); Employee e1 = new Employee(); e1.Name = "Test"; e1.Age = 27; var valueEqual1 = e.Equals(e1); Console.WriteLine(valueEqual1); Console.ReadLine(); } } class Employee{ public int Age { get; set; } public string Name { get; set; } public override bool Equals(object? obj){ if (obj == null) return false; if (this.GetType() != obj.GetType()) return false; Employee p = (Employee)obj; return (this.Age == p.Age) && (this.Name == p.Name); } public override int GetHashCode(){ return Age.GetHashCode() ^ Name.GetHashCode(); } }
输出
True True
广告