Are there special operators in c # for determining whether a number belongs to a set? The analogue in pascal: 2 in [1..4], returns true in this case.

    2 answers 2

    Alternatively, you can write a method or even an extension method:

    public static class RangeHelper { public static bool IsInRange(this int x, int start, int end) => x >= start && x <= end; } 

    Use this:

     if (2.IsInRange(1, 4)) { ... } 

    Screenshot

      In C #, there is a type that represents a set. For your case, the analog is HashSet<int> . Check entry is trivial:

       var x = new HashSet<int>() { 2, 3, 4 }; Console.WriteLine(x.Contains(2)); // выдаёт True 

      But there are no such convenient set literals, as in Pascal, in C #, the closest analogue [2..4] is

       new HashSet<int>(Enumerable.Range(start: 2, count: 3)) 

      There is currently no special support for sets at the syntax level in C #.