更新ConcurrentDictionary的最简单方法是什么

时间:2014-06-26 14:57:17

标签: c# concurrency concurrentdictionary

我创建了一个ConcurrentDictionary但不确定如何更新它的元素:

        public class ModelClient : ICloneable
        {
            public Session session;
            public List<string> keys = new List<string>();
            public List<ModelOrder> orders = new List<ModelOrder>();

            public ModelClient(SessionID sessionID)
            {
                session = Session.LookupSession(sessionID);
            }

            public object Clone() { return this.MemberwiseClone(); }
        }

      public class ModelOrder : ICloneable
        {
            private string Symbol;
            private int Amount;
            private double Price;

            public ModelOrder(string Symbol, int Amount, double Price)
            {
                this.Symbol = Symbol;
                this.Amount = Amount;
                this.Price = Price;
            }

            public object Clone() { return this.MemberwiseClone(); }
        }

public ConcurrentDictionary<SessionID, ModelClient> ModelClients = new ConcurrentDictionary<SessionID, ModelClient>();

这是我需要更新的地方(通过向订单添加新元素),看来我应该使用TryUpdate,但它看起来非常麻烦且难以阅读。

请有人给我一个好方法。

   public bool AddModelClientOrder(SessionID sessionID, string orderMessage)
    {
        string[] part = orderMessage.Split(' ');

        //HOW DO I BEST DO THIS?
        ModelClients[sessionID].orders.Add(new ModelOrder(part[0], int.Parse(part[1]), double.Parse(part[2])));
    }

0 个答案:

没有答案