📌  相关文章
📜  检查给定范围在C#中是否相等

📅  最后修改于: 2021-05-29 23:49:20             🧑  作者: Mango

范围结构在C#8.0中引入。它表示具有开始索引和结束索引的范围。您可以借助Range结构提供的以下方法来检查给定的范围是否相等:

1. Equals(Object):此方法是Object类方法,它返回一个值,该值表明给定范围对象等于另一个范围对象。如果返回true,则结果为布尔形式,则当前范围对象等于另一个范围对象;如果返回false,则当前范围对象不等于另一个范围对象。

句法:

public override bool Equals(System::Object ^ value);

例子:

// C# program to illustrate how to 
// check the given ranges is equal or not
// Using Equals(object) method of Range struct
using System;
  
namespace range_example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating a range
        // using StartAt() method
        var r1 = Range.StartAt(3);
        var r2 = Range.StartAt(4);
  
        // Checking the ranges
        // are equal or not
        // Using Equals(Object) method
        if (r1.Equals(r2)) {
  
            Console.WriteLine("Both range 1{0} and range"+
                              " 2 {1} are equal", r1, r2);
        }
  
        else {
  
            Console.WriteLine("Both ranges are not equal");
        }
    }
}
}

输出:

Both ranges are not equal

2. Equals(Range):此方法返回一个值,该值显示给定的对象等于另一个对象。如果结果为true,则结果为bool形式,则当前对象等于另一个范围对象;如果结果为false,则当前对象不等于另一个范围对象。

例子:

// C# program to illustrate how to 
// check the given ranges is equal or not
// Using Equals(Range) method of Range struct
using System;
  
namespace range_example {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating a ranges
        Range r1 = 1..8;
        Range r2 = 1..8;
  
        // Checking the ranges
        // are equal or not
        // Using Equals(Range) method
        if (r1.Equals(r2)) {
  
            Console.WriteLine("Both ranges are equal");
        }
  
        else {
  
            Console.WriteLine("Both ranges are not equal");
        }
    }
}
}

输出:

Both ranges are equal