【问题标题】:ArrayList of Hashtables in C#C#中的哈希表的ArrayList
【发布时间】:2011-03-25 18:44:49
【问题描述】:

我想要一个包含 HashTable 的 ArrayList。我创建了一个哈希表并添加了值。然后我将它添加到 ArrayList。然后我更改了哈希表的值并再次将其添加到数组列表中。它不保存第一个值,并以与最后一个值完全相同的重复值结尾!

有什么建议吗?这是我的代码

namespace ValuesTest
{
    internal class Class1
    {    
        public static ArrayList StartList = new ArrayList();
        public static Hashtable Start = new Hashtable();    

        static void Main(string[] args)
        {    
            Start["active"] = true;
            Start["name"] = "prog1";
            Start["path"] = @"C:\programfiles\prog1";
            Start["parameter"] = string.Empty;

            StartList.Add(Start);

            Start["active"] = false;
            Start["name"] = "prog2";
            Start["path"] = @"C:\programfiles\prog2";
            Start["parameter"] = "/q";

            StartList.Add(Start);

            foreach (Hashtable HT in StartList)
            {
                Console.WriteLine(HT["active"] + " - " + HT["name"] + " - " + HT["path"] + " - " + HT["parameter"]);
                // it will always gives
                // False - prog2 - C:\programfiles\prog2 - /q    
            }

            Console.ReadLine();   
        }
    }
}

【问题讨论】:

  • 我最近看到了很多ArrayList。相反,您应该使用 List ,除非您使用的是 .Net 框架 1/1.1。 List 将提供更好的性能并避免强制转换('foreach' 在您的情况下是隐含的)。问候
  • 您不会创建 Hashtable 的新实例,而是覆盖值。 P.S.:如果您期待答案,则在主题中使用多个感叹号并没有太大帮助:p
  • 不过,人们应该对问题而不是语法进行投票。

标签: c# arraylist hashtable


【解决方案1】:

对此的更多反馈:尽管 Nix 已经回答了您的问题,但我也会放弃 Hashtable(就像 ArrayList 它已经过时一样),尽管您的概念并不适合它:您将字符串存储到对象..

所以,我们最终会得到复制和更改

 static void Main(string[] args)
 {
        Dictionary<string, object> Start = new Dictionary<string, object>();
        Start["active"] = true;
        Start["name"] = "prog1";
        Start["path"] = @"C:\programfiles\prog1";
        Start["parameter"] = string.Empty;

        StartList.Add(Start);

        Dictionary<string, object> Start = new Dictionary<string, object>();
        Start["active"] = false;
        Start["name"] = "prog2";
        Start["path"] = @"C:\programfiles\prog2";
        Start["parameter"] = "/q";

        StartList.Add(Start);

但我会更进一步:如果您只想生成进程,这就是 Process 类的用途。它在StartInfo 属性中保留相同的信息(“活动”除外)。

另一种(更好的?)方法是为这组信息创建一个值类:

class YourStartInfo
{
  public bool Active { get; set; }
  public string Name { get; set; }
  public string Path { get; set; }
  public string Parameter { get; set; }
}

并更改您的代码以利用它:

 static List<YourStartInfo> StartList = new List<YourStartInfo>();

 static void Main(string[] args)
 {    
        StartList.Add(new YourStartInfo {
            Active = true,
            Name = "prog1",
            Path = @"C:\programfiles\prog1";
            Parameter = string.Empty
        });

        StartList.Add(new YourStartInfo {
            Active = false,
            Name = "prog2",
            Path = @"C:\programfiles\prog2";
            Parameter = "/q"
        });

        foreach (YourStartInfo startInfo in StartList)
        {
            // Access the information in a sane way, not as object here
            if (startInfo.Active)
            {
                 // Probably launch it?
            }
        }

        Console.ReadLine();   
    }

【讨论】:

  • 实际上,我发现自己现在正在做的事情 :-) 但感谢您的建议 :-) 它给了我一个很好的推动,我在正确的道路上,干杯。
【解决方案2】:

您正在修改 唯一 Start Hashtable 对象,您必须创建一个新对象:

Start = new Hashtable();

就在第一个之后:

StartList.Add(Start);

请记住,您只是向ArrayList 的对象添加引用:所以您的代码所做的是:填充哈希表,向列表添加对它的引用,修改它更多,然后再次添加相同的引用。


想补充一下,你为什么使用哈希表?将新类与您想要的字段一起使用会好得多 - 然后它们可以有一个 PrintInfo 或一个 ToString 覆盖来获取您需要的信息 - 并且可能是一个 Execute 方法。

【讨论】:

【解决方案3】:

将 start 移到 Main 函数内部,并为第二个 add 重新初始化它(正如 @lasseespeholt 所说,使用 List&lt;T&gt;)。

    static List<Hashtable> StartList = new List<Hashtable>();

    static void Main(string[] args)
    {
        Hashtable Start = new Hashtable();
        Start["active"] = true;
        Start["name"] = "prog1";
        Start["path"] = @"C:\programfiles\prog1";
        Start["parameter"] = string.Empty;

        StartList.Add(Start);

        Start = new Hashtable();
        Start["active"] = false;
        Start["name"] = "prog2";
        Start["path"] = @"C:\programfiles\prog2";
        Start["parameter"] = "/q";

        StartList.Add(Start);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2013-08-20
    • 2012-10-03
    • 2019-03-26
    • 2011-02-15
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多