我試圖做的是讓群聊的在線成員留在記憶中。我定義了一個靜態嵌套字典,如下所示:private static ConcurrentDictionary<string, ConcurrentDictionary<string, ChatMember>> onlineGroupsMembers = new ConcurrentDictionary<string, ConcurrentDictionary<string, ChatMember>>();然后,當新成員到達時,我添加它: onlineGroupsMembers.AddOrUpdate (chatKey, (k) => // add new { var dic = new ConcurrentDictionary<string, ChatMember>(); dic[chatMember.Id] = chatMember; return dic; }, (k, value) => // update { value[chatMember.Id] = chatMember; return value; });現在的問題是,如何從內部字典中刪除成員?還如何在外部字典中刪除空的字典?并發字典有 TryRemove,但它沒有幫助,檢查 ContainsKey 然后刪除它不是原子的。謝謝。
如何從嵌套的并發詞典中刪除項目?
慕田峪4524236
2022-09-04 16:54:27