Specifying Ranges in C#

后端 未结 3 1037
遇见更好的自我
遇见更好的自我 2021-01-23 02:46

I have 6 Ranges:

1000000-5000000
50000001-10000000
10000001-25000000
25000001-50000000
50000001-75000000
75000001-100000000

Now how do I say th

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-23 03:19

    You can declare class about this:

    public class Range
    {
        public int Min { get; set; }
        public int Max { get; set; }
        public int Limit { get; set; }
        public int Result{get;set;}
    
        public Range(int min, int max, int limit, int result)
        {
            this.Min = min;
            this.Max = max;
            this.Limit = limit;
            this.Result = result;
        }
    
        public bool InRange(int value)
        {
            if (value >= this.Min && value <= this.Max && value <= limit)
                return true;
            return false;
        }
    }
    

    and use this class like:

    List rangeList = new List();
            rangeList.Add(new Range(1000000, 5000000, 10000000, 10));
            rangeList.Add(new Range(5000001, 10000000, 10000000, 20));
            int? result = rangeList.Where(t => t.InRange(value)).Select(t => t.Result).FirstOrDefault();
    

    if variable result is not null, it's your final result.

提交回复
热议问题