How to use both Take and Skip operators in LINQ C# programming

WBOY
Release: 2023-09-06 16:45:07
forward
851 people have browsed it

如何在 LINQ C# 编程中同时使用 Take 和 Skip 运算符

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.

Example 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; }
}
Copy after login

Output

True
False
Copy after login

The Chinese translation of Example 2

is:

Example 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();
   }
}
Copy after login

Output

True
True
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template