开发者

Collection with 10 elements (only)

开发者 https://www.devze.com 2023-01-15 03:37 出处:网络
I would like to make a score list with only 10 elements. Basically, simple collection which adds the value. If new value is higher than other one it adds one and last one get out. (everyone knows how

I would like to make a score list with only 10 elements. Basically, simple collection which adds the value. If new value is higher than other one it adds one and last one get out. (everyone knows how it looks like :) )

Secondly, I need a list of 5 lastest values that have been changed, something like history panel.

All in all, both are very s开发者_如何学JAVAimilar - a list with limited items.

Is there a neat pattern for these? Some cool snippet? I need to use Silverlight for WP7 and the low power consumption solution would be great. Should I make my own collecion? Derive from one or implement interface. Thx in advance.


I think System.Collections.Generic.Queue<T> is exactly what you want.


I did something like this to limit it to 15. It seems to be no OrderBy in WP7 :/

    public void SaveScore(ScoreInfo scoreInfo)
    {
        var listOfScoreInfo = this.GetListOrDefault<ScoreInfo>(App.SCORE);
        bool isAdd = true;
        foreach (var info in listOfScoreInfo)
        {
            if (info.Name == scoreInfo.Name && info.Score == scoreInfo.Score)
                isAdd = false;
        }
        if(isAdd)
            listOfScoreInfo.Add(scoreInfo);
        listOfScoreInfo.Sort(scoreInfo.Compare);

        if (listOfScoreInfo.Count > 15)
        {
            listOfScoreInfo.RemoveAt(15);
        }

        this.AddOrUpdateValue(App.SCORE, listOfScoreInfo);
        this.Save();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消