A dictionary object that uses ranges of values for keys

前端 未结 8 827
情深已故
情深已故 2020-11-27 19:26

I have need of a sort of specialized dictionary. My use case is this: The user wants to specify ranges of values (the range could be a single point as well) and assign a v

8条回答
  •  粉色の甜心
    2020-11-27 20:15

    This isn't exactly what you want but I think it may be the closest you can expect.

    You can of course do better than this (Was I drinking earlier?). But you have to admit it is nice and simple.

    var map = new Dictionary, double>()
    {
        { d => d >= 0.0 && d <= 10.0, 9.0 }
    };
    
    var key = map.Keys.Single(test => test(1.0))
    var value = map[key];
    

提交回复
热议问题