Dictionary with Func as key

前端 未结 3 987
失恋的感觉
失恋的感觉 2020-12-31 12:50

I am wondering if this is a sane choice of key for a dictionary? What I want to do is use an expression as the key in a dictionary, something like:

    var m         


        
3条回答
  •  自闭症患者
    2020-12-31 13:12

    Considering the way that you use your map, you will be better off with a List,int>>, because the order of checking the lambdas will no longer be arbitrary, as in a hash-based dictionary. This approach also lets you skip the lookup step:

    var map3 = new List,int>> {
        new Tuple,int>((x) => x % 2 == 0, 1)
    ,   new Tuple,int>((x) => x % 10 == 0, 2)
    };
    var t = map3.SingleOrDefault(t => t.Item1(2));
    if (t != null) {
        var v = t.Item2;
    }
    

提交回复
热议问题