Get Max value from List

后端 未结 8 413
猫巷女王i
猫巷女王i 2021-02-02 05:08

I have List List, my type contains Age and RandomID

Now I want to find the maximum age from this list.

What

8条回答
  •  既然无缘
    2021-02-02 05:47

    Easiest way is to use System.Linq as previously described

    using System.Linq;
    
    public int GetHighestValue(List list)
    {
        return list.Count > 0 ? list.Max(t => t.Age) : 0; //could also return -1
    }
    

    This is also possible with a Dictionary

    using System.Linq;
    
    public int GetHighestValue(Dictionary obj)
    {
        return obj.Count > 0 ? obj.Max(t => t.Key.Age) : 0; //could also return -1
    }
    

提交回复
热议问题