Home  >  Article  >  Backend Development  >  How to use both Take and Skip operators in LINQ C# programming

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

WBOY
WBOYforward
2023-09-06 16:45:07727browse

如何在 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; }
}

Output

True
False

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();
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete