Nếu bạn không nhớ tái tạo lại từ điển, bạn có thể sử dụng một Statment LINQ.
var dictionary = new SortedDictionary<int, SortedDictionary<string, List<string>>>();
var insertAt = 10;
var newValues = dictionary.ToDictionary(
x => x.Key < insertAt ? x.Key : x.Key + 1,
x => x.Value);
return new SortedDictionary<int, SortedDictionary<string, List<string>>>(newValues);
hoặc
var dictionary = new SortedDictionary<int, SortedDictionary<string, List<string>>>();
var insertAt = 10;
var newValues = dictionary.ToDictionary(
x => x.Key < insertAt ? x.Key : x.Key + 1,
x => x.Value);
dictionary.Clear();
foreach(var item in newValues) dictionary.Add(item.Key, item.Value);
Tại sao này bình chọn xuống? – keyboardP
Bạn không thể sửa đổi khóa. Thay vào đó, hãy xóa khóa và thêm một giá trị mới cho khóa + 1 –
Câu hỏi này có vẻ như bạn đang cố gắng chèn một mục theo cách thủ công và giữ cho nó được sắp xếp (cụ thể, tạo khoảng trống cho một mục). Lưu ý SortedDictionary thực hiện điều này cho bạn. – Russell