代码之家  ›  专栏  ›  技术社区  ›  Chud37

修改列表中的最后一项[已关闭]

c#
  •  -6
  • Chud37  · 技术社区  · 6 年前

    我有一个 List<string, int> 我想修改最后一项的整数。

    我该怎么做?

    我找到了方法 .Last<T>

    1 回复  |  直到 6 年前
        1
  •  1
  •   Matthew Watson    6 年前

    从您的评论中,您正在使用 SortedList<string, int> .Keys[] 数组来访问 SortedList ,就像这样:

    using System;
    using System.Collections.Generic;
    
    namespace Demo
    {
        static class Program
        {
            public static void Main()
            {
                var test = new SortedList<string, int>();
    
                test.Add("one", 1);
                test.Add("two", 2);
    
                test[test.Keys[test.Count - 1]] = 3;
    
                foreach (var item in test)
                {
                    Console.WriteLine($"test[{item.Key}] == {item.Value}");
                }
            }
        }
    }
    

    这张照片:

    test[one] == 1
    test[two] == 3