【问题标题】:List<string> items disappearring in c#列表<字符串>项目在c#中消失
【发布时间】:2015-01-09 18:34:16
【问题描述】:

我有一个简单的 List 包装类:

class ExtensionList
{
    public List<string> Extensions;

    public ExtensionList()
    {
        Extensions = new List<string>();
    }

    public void push(params string[] ext)
    {
        // Add the * extension if "ALL__" is pushed.
        if (ext.Contains("ALL__"))
        {
            Extensions = new List<string>();
            Extensions.Add("*");
            return;
        }

        // Don't add duplicate if pushed extension is already in list.
        if (Extensions.Intersect(ext).Any())
            return;

        Extensions.AddRange(ext);
    }
}

以及一个实例化“Rec”的循环,每个循环都有上述列表对象之一。

public bool readAppConfig(string appConfigPath)
    {
        Recs = new Dictionary<string, List<Rec>>()
        {
            {"Headers",    new List<Rec>()},
            {"Ignores",    new List<Rec>()},
            {"Exceptions", new List<Rec>()}
        };

        foreach (string sectionName in Recs.Keys)
        {
            var recSection = ConfigurationManager.GetSection(sectionName) as NameValueCollection;
            foreach (string key in recSection.AllKeys)
            {
                Rec r = new Rec();
                r.strMatch = key;
                r.ExtList.push(recSection[key].Split(';', ' '));
                Recs[sectionName].Add(r);
            }
        }

        return true;
    }

这个问题非常简单,而且非常奇怪(至少对我而言)。 当调用 r.ExtList.push(...) 时,我可以使用 Visual Studio 跟踪执行到 push() 函数中,并看到正确的字符串被添加到 ExtList 中的“扩展”中(通过将鼠标悬停在它上面) .然后,当我继续使用 Visual Studio 逐步执行代码时,push() 函数返回到 foreach 循环。从那一刻起,这个 Rec 的 ExtList 不再有任何元素。 push() 返回后它们都消失了。

为了完整性,这里是 Rec 结构体。

struct Rec
{
    public string strMatch;
    ExtensionList extList;
    public ExtensionList ExtList {
        get { return extList??new ExtensionList();}
        set { extList = value; }
    }
};

【问题讨论】:

  • get { return extList??new ExtensionList();} 未分配给私有字段

标签: c# string list


【解决方案1】:

ExtList 属性的 getter 返回值,或者如果它为 null,则返回一个新列表。然后调用对象的 getter 并改变该列表。该列表从未设置为extList 属性。无论是属性 getter,还是初始化 Rec 的代码,都不会设置该字段。

令人惊讶的是,这实际上与你有一个可变结构这一事实没有任何关系,也就是说,可变结构仍然是邪恶的,你应该像瘟疫一样避免它们。您的 struct 应该是不可变的,或者是 class

【讨论】:

    【解决方案2】:

    我看不出ExtensionsList 类的原因。如前所述,逻辑可以直接在Rec 中实现。

    public struct Rec
    {
        public Rec(string match) : this()
        {
            Match=match;
            Extensions=new List<string>();
        }
        public string Match { get; private set; }
        public List<string> Extensions { get; private set; }
        public void Push(params string[] ext)
        {
            if(ext.Contains("ALL__"))
            {
                Extensions=new List<string>() { "*" };
                return;
            }
    
            if(Extensions.Intersect(ext).Any())
            {
                return;
            }
    
            Extensions.AddRange(ext);
        }
    }
    

    类似用法:

        public bool readAppConfig(string appConfigPath)
        {
            Recs=new Dictionary<string, List<Rec>>()
            {
                {"Headers",    new List<Rec>()},
                {"Ignores",    new List<Rec>()},
                {"Exceptions", new List<Rec>()}
            };
    
            foreach(string sectionName in Recs.Keys)
            {
                var recSection=ConfigurationManager.GetSection(sectionName) as NameValueCollection;
                foreach(string key in recSection.AllKeys)
                {
                    Rec r=new Rec(key);
                    r.Push(recSection[key].Split(';', ' '));
                    Recs[sectionName].Add(r);
                }
            }
    
            return true;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-04-28
      • 2010-10-09
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多