Car*_*enK 41 .net vb.net generics
通常情况下,我会尽可能选择List<String>[或者,在VB中List(Of String)] StringCollection:另请参阅最佳字符串容器.
然而,看起来,仿制药 - 并且因此List<String>- 显然在VS 2008的设置设计器中不受支持.因此,如果我想在我的用户设置中使用字符串列表,我不得不求助于使用StringCollection那里.
现在因为我不希望在StringCollection整个代码中看到,我需要将其转换为List<String>.我该如何有效地做到这一点?或者,甚至更好,我错了,有一种方法可以List<String>在设置设计师中使用?
编辑:我必须使用.NET 2.0.
Nol*_*rin 104
转换StringCollection到List<string>,如果你正在使用.NET 3.5是非常简单的.
var list = stringCollection.Cast<string>().ToList();
Run Code Online (Sandbox Code Playgroud)
关于你的第二个问题,我似乎记得可以List<string>在Visual Studio的设置设计器中使用它.如果您选择自定义类型然后浏览器到适当的类型,它应该工作.但是,我可能会误解,所以我需要验证它.
Phi*_*ice 16
如果您有使用.NET 2.0,我会想象cleanliest选择是创建一个包装StringCollection实现IEnumerable<string>和IEnumerator<string>用于StringCollection和StringEnumerator分别.(注意:根据元数据,StringEnumerator不实现IEnumerator).以下示例.然而,在一天结束的时候,有人在什么地方将被做foreach()在StringCollection,所以一个可能会认为,一个简单的foreach(string item in stringCollection)并增加了List<string>就足够了; 我怀疑这不足以满足您的需求.
您也可以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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 C# 3.0 和 .NET 3.5,则可以使用以下命令创建列表
var list = new List<string>(new StringCollection().Cast<string>());
Run Code Online (Sandbox Code Playgroud)
另一种方法是:
var c = new StringCollection();
c.AddRange(new List<string>().ToArray());
Run Code Online (Sandbox Code Playgroud)
为什么不保持简单,只是迭代它并将项目添加到列表中,或者我误解了什么?
public static List<string> Convert(StringCollection collection)
{
List<string> list = new List<string>();
foreach (string item in collection)
{
list.Add(item);
}
return list;
}
Run Code Online (Sandbox Code Playgroud)