转换StringCollection列出&LT;字符串&GT; [英] Convert StringCollection to List<String>
问题描述
通常情况下,我会选择名单,其中,字符串&GT;
[或者,在VB中,列表(串)
]在 StringCollection
尽可能:又见的 <一个href="http://stackoverflow.com/questions/669106/best-string-container-stringcollection-collectionstring-liststring-arraylist">Best字符串集装箱 的。
Normally, I'd choose List<String>
[or, in VB, List(Of String)
] over StringCollection
whenever possible: see also Best string container.
不过,因为它似乎,仿制药 - 因此,名单,其中,字符串&GT;
- 显然不是在VS 2008的设置,设计支持。所以,如果我想用在我的用户设置字符串列表,我不得不求助于使用 StringCollection
那里。
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 StringCollection
there.
现在,因为我不希望看到 StringCollection
在我的code,我需要将其转换为名单,其中,字符串&GT;
。如何做到这一点有效?或者,甚至更好,我是错误的,有一种方法使用名单,其中,字符串&GT;
的设置设计师
Now as I don't want to see StringCollection
throughout 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?
编辑:我必须使用.NET 2.0 的
推荐答案
如果您的有无的使用.NET 2.0,我会想象cleanliest选项是创建一个包装 StringCollection
实现的IEnumerable&LT;字符串&GT;
和的IEnumerator&LT;字符串&GT;
的 StringCollection $ C $
StringEnumerator
分别为C>和。 (注意的:根据元数据, StringEnumerator
不执行的IEnumerator
)。示例如下。然而,在一天结束的时候,有人在什么地方是打算做一个的foreach()
在 StringCollection
,所以一的可以的认为,一个简单的的foreach(字符串stringCollection项目)
,并增加了名单,其中,串&GT;
就足够了;我怀疑这会不会是performat足以满足您的需求。
If you have to use .NET 2.0, I would imagine the cleanliest option would be to create a wrapper for StringCollection
that implements IEnumerable<string>
and IEnumerator<string>
for StringCollection
and StringEnumerator
respectively. (note: according to metadata, StringEnumerator
does 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 could argue 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.
您也可以实施的IList&LT;字符串&GT;
使用这种方式为好,以节省您复制标的字符串,但是你将支付具有包装的处罚类型的委托调用(堆栈上的一个方法调用!)。我建议你无论如何对待你的系统内的事情,术语接口 IEnumberable&LT;字符串&GT;
,的IList&LT;字符串&GT;
而不是具体的名单等,它会引导你失望的路径,以更大的灵活性。
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.
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();
}
}
这篇关于转换StringCollection列出&LT;字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!