Problem with dictionary <string, List <IRp>>, data rewriting

Hi, I kept the chat dialog in the property type:

  public Dictionary<string, List<IRp>> History { get; set; }

The key is a nickname and a list is a collection that contains a history of communication.

I use this SaveRp method for storage, IRp is the interface that implements the message class.

    public void SaveRp(IRp rp)
    {
        if (!History.ContainsKey(rp.Nick))
        {
            History.Add(rp.Nick, new List<IRp> { rp });
        }
        else
        {
            History[rp.Nick].Add(rp);
        }
    }

The problem is that I am storing messages in this sequence for the same Nick (Tom, for example):

1 text message: Hello

2 message text: how

3 Message text:

4 text message: you?

The problem is that I load the history for this nickname using this method:

    public IList<IRp> GetRps(string nick)
    {
        var result=new List<IRp>();
        if (History.ContainsKey(nick))
        {
           result= History[nick];
        }
        return result;
    }

I get this:

1 text message: you?

2 text message: you?

3 message text: you?

4 text message: you?

I think the error should be in the SaveRp method and somehow rewrite / rewrite

+3
source share
1

, , Rp, . , , , , .

Rp .

, , , , - - , , .

+7

Source: https://habr.com/ru/post/1789482/


All Articles