.net 将 StringCollection 转换为 List<String>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/844412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:40:06  来源:igfitidea点击:

Convert StringCollection to List<String>

.netvb.netgenerics

提问by CarstenK

Normally, I'd choose List<String>[or, in VB, List(Of String)] over StringCollectionwhenever possible: see also Best string container.

通常,我会尽可能选择List<String>[或,在 VB 中,List(Of String)] over StringCollection:另请参阅Best string container

However, as it seems, generics — and hence, List<String>— are apparently not supported in VS 2008's settings designer. Therefore, if I want to use a list of strings in my user settings, I have to resort to using a StringCollectionthere.

然而,看起来,List<String>VS 2008 的设置设计器显然不支持泛型——因此,——。因此,如果我想在我的用户设置中使用一个字符串列表,我必须求助于使用StringCollection那里。

Now as I don't want to see StringCollectionthroughout my code, I need to convert it to List<String>. How do I do this efficiently? Or, even better, am I mistaken and there is a way to use List<String>in settings designer?

现在因为我不想StringCollection在我的代码中看到所有内容,所以我需要将它转换为List<String>. 我如何有效地做到这一点?或者,更好的是,我错了,有一种方法可以List<String>在设置设计器中使用吗?

EDIT: I have to use .NET 2.0.

编辑:我必须使用 .NET 2.0。

采纳答案by Phil Price

If you haveto use .NET 2.0, I would imagine the cleanliest option would be to create a wrapper for StringCollectionthat implements IEnumerable<string>and IEnumerator<string>for StringCollectionand StringEnumeratorrespectively. (note: according to metadata, StringEnumeratordoes not implement IEnumerator). Sample below. However, at the end of the day, someone, somewhere is going to be doing a foreach()over the StringCollection, so one couldargue that a simple foreach(string item in stringCollection)and adding to the List<string>would suffice; I doubt this wouldn't be performat enough for your needs.

如果您使用.NET 2.0,我会想象cleanliest选择是创建一个包装StringCollection实现IEnumerable<string>IEnumerator<string>用于StringCollectionStringEnumerator分别。(注意:根据元数据,StringEnumerator未实现IEnumerator)。示例如下。然而,在一天结束的时候,有人在什么地方将被做foreach()StringCollection,所以一个可能会认为,一个简单的foreach(string item in stringCollection)并增加了List<string>就足够了; 我怀疑这不足以满足您的需求。

You could also implement IList<string>using this approach as well, to save you duplicating the underlying strings, but you'll pay a penalty of having the "wrapper" type delegate calls (one more method call on the stack!). I would suggest you treat things in-terms of interfaces within your system anyway IEnumberable<string>, IList<string>etc, instead of the concrete List, it will guide you down a path to greater flexibility.

您也可以IList<string>使用这种方法来实现,以节省重复底层字符串,但您将支付“包装器”类型委托调用(堆栈上的另一个方法调用!)的惩罚。我建议你把在-方面的接口事物系统内反正IEnumberable<string>IList<string>等等,而不是具体的列表,它会引导你到更大的灵活性的路径。

    static void Main(string[] args)
    {
        StringCollection stringCollection = new StringCollection();
        stringCollection.AddRange(new string[] { "hello", "world" });

        // Wrap!
        List<string> listOfStrings = new List<string>(new StringCollectionEnumerable(stringCollection));

        Debug.Assert(listOfStrings.Count == stringCollection.Count);
        Debug.Assert(listOfStrings[0] == stringCollection[0]); 

    }

    private class StringCollectionEnumerable : IEnumerable<string>
    {
        private StringCollection underlyingCollection; 

        public StringCollectionEnumerable(StringCollection underlyingCollection)
        {
            this.underlyingCollection = underlyingCollection; 
        }

        public IEnumerator<string> GetEnumerator()
        {
            return new StringEnumeratorWrapper(underlyingCollection.GetEnumerator()); 
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator(); 
        }
    }

    private class StringEnumeratorWrapper : IEnumerator<string>
    {
        private StringEnumerator underlyingEnumerator; 

        public StringEnumeratorWrapper(StringEnumerator underlyingEnumerator)
        {
            this.underlyingEnumerator = underlyingEnumerator;
        }

        public string Current
        {
            get
            {
                return this.underlyingEnumerator.Current; 
            }
        }

        public void Dispose()
        {
            // No-op 
        }

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return this.underlyingEnumerator.Current;
            }
        }

        public bool MoveNext()
        {
            return this.underlyingEnumerator.MoveNext(); 
        }

        public void Reset()
        {
            this.underlyingEnumerator.Reset(); 
        }
    }

回答by Noldorin

Converting StringCollectionto List<string>is wonderfully simple if you're using .NET 3.5.

转换StringCollectionList<string>,如果你正在使用.NET 3.5是非常简单的。

var list = stringCollection.Cast<string>().ToList();

Regarding your second question, I do seem to recollect that it is possible to use List<string>in the settings designer of Visual Studio. If you select custom type then browser to the appropiate type, it should work. However, I could be mistaken on that, so I'll need to verify it.

关于你的第二个问题,我似乎记得可以List<string>在Visual Studio的设置设计器中使用。如果您选择自定义类型,然后浏览器选择合适的类型,它应该可以工作。但是,我可能会弄错,所以我需要验证一下。

回答by Tasawer Khan

stringCollection.Cast<string>().ToList()

stringCollection.Cast<string>().ToList()

回答by Patrik Svensson

Why not keep it simple and just iterate though it and add the items to the list, or have i misunderstood something?

为什么不保持简单,只是迭代它并将项目添加到列表中,或者我误解了什么?

public static List<string> Convert(StringCollection collection)
{
    List<string> list = new List<string>();
    foreach (string item in collection)
    {
        list.Add(item);
    }
    return list;
}

回答by Jerome Laban

If you're using C# 3.0 and .NET 3.5, you can create a list using

如果您使用的是 C# 3.0 和 .NET 3.5,则可以使用创建列表

var list = new List<string>(new StringCollection().Cast<string>());

The other way around is :

另一种方式是:

var c = new StringCollection();
c.AddRange(new List<string>().ToArray());

回答by krlzlx

C# 2.0

C# 2.0

StringCollection colString = new StringCollection();
string[] arrString = new string[colString.Count];
colString.CopyTo(arrString, 0);
List<string> lstString = new List<string>(arrString);