We are creating two instances of Employee class, e and e1. e is assigned to e1. Both objects point to the same reference, so we will get true For all Equals, expected output.
In the second case, we can observe that although the attribute values are the same Equals returns false. Basically, when parameters refer to different objects Equals does not check the value and always returns false.
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
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
The above is the detailed content of How to use both Take and Skip operators in LINQ C# programming. For more information, please follow other related articles on the PHP Chinese website!