StringCollection FAQ [C#, BCL]
?
StringCollection FAQ [C#, BCL]
?
Updated on Monday, March 21, 2005
?
Written by Allen Lee
?
Q:如何使用StringCollection[1]?
A:通常我們有三種方法來訪問StringCollection里面的string元素:
?
//?Code?#01StringCollection?sc?=?new?StringCollection();
sc.AddRange(textBox1.Lines);
//?StringCollection?used?together?with?StringEnumerator.
StringEnumerator?se?=?sc.GetEnumerator();
while?(se.MoveNext())
{
????Console.WriteLine(se.Current);
}
//?'foreach'?syntax?used.
foreach(string?str?in?sc)
{
????Console.WriteLine(str);
}
//?'for'?syntax?used.
for(int?i?=?0;?i?<?sc.Count;?i++)
{
????Console.WriteLine(sc[i]);
}
?
Q:與ArrayList相比,StringCollection有什么優(yōu)勢?
A:首先讓我們來看看如下代碼:
?
//?Code?#02//?StringCollection?used?for?Strings?operation.
StringCollection?sc?=?new?StringCollection();
sc.AddRange(textBox1.Lines);
foreach?(string?str?in?sc)
{
????if?(str.Trim().StartsWith("File"))
????{
????????//?Do?something
????}
????else?if?(str.Trim().StartsWith("Registry"))
????{
????????//?Do?something?else
????}
}
//?ArrayList?used?for?Strings?operation.
ArrayList?al?=?new?ArrayList();
al.AddRange(textBox1.Lines);
foreach?(object?obj?in?al)
{
????string?str?=?(string)obj;
????if?(str.Trim().StartsWith("File"))
????{
????????//?Do?something
????}
????else?if?(str.Trim().StartsWith("Registry"))
????{
????????//?Do?something?else
????}
}
?
從上面的代碼我們看可以看出,用StringCollection寫出的代碼更加直觀(尤其在你要對集合里面的String元素進(jìn)行復(fù)雜的操作的時候),清楚地表明了集合里面的元素的性質(zhì),并且免除我們手動進(jìn)行強類型轉(zhuǎn)換。
Q:StringCollection是否為string作了專門的優(yōu)化?
A:沒有!這可能是一個最大的誤解,別看到StringCollection只用來儲存string就以為它為string的儲存作了專門的優(yōu)化!
雖然在某程度上用StringCollection寫的代碼比ArrayList的更加直觀,然而后者卻在性能上稍勝一籌[2]。事實上,StringCollection只是對ArrayList進(jìn)行了一番裝裱而已(以下是使用Reflector反編譯的StringCollection代碼片斷):
?
//?Code?#03[Serializable]
public?class?StringCollection?:?IList,?ICollection,?IEnumerable
{
????//?Methods
????public?void?AddRange(string[]?value)
????{
????????if?(value?==?null)
????????{
????????????throw?new?ArgumentNullException("value");
????????}
????????this.data.AddRange(value);
????}
????//?Other?methods?omitted
????//?Properties
????public?string?this[int?index]
????{
????????get?{?return?(string)?this.data[index];?}
????????set?{?this.data[index]?=?value;?}
????}
????//?Other?properties?omitted
????//?Fields
????private?ArrayList?data;
}
?
從代碼中我們可以看到StringCollection實質(zhì)上把其所提供的功能重定向給ArrayList,它們的關(guān)系是Use-A。
Q:在什么情況下我們應(yīng)該使用StringCollection呢?
A:這個問題沒有標(biāo)準(zhǔn)答案,視你所要處理的問題和你的處理原則而定。首先,讓我們來聽聽Robert B. Murray的想法:
程序的正確性要比它的速度更重要。和人力資源相比,計算機的運行時間的開銷會變得越來越便宜;對于那些在提高性能的同時還會導(dǎo)致程序的可理解性和可維護(hù)性變差的方法,我們應(yīng)該持謹(jǐn)慎對待。[3]于是,一般說來,使用StringCollection而不是ArrayList是有一定作用的。但就我個人來說,我已經(jīng)習(xí)慣了使用ArrayList。當(dāng)然,如果你使用的是.NET 2.0,那么List會是你不二的選擇,它結(jié)合了StringCollection和ArrayList的優(yōu)點——代碼直觀和性能優(yōu)良!
?
//?Code?#04List<string>?ls?=?new?List<string>();
ls.AddRange(textBox1.Lines);
foreach?(string?str?in?ls)
{
????if?(str.Trim().StartsWith("File"))
????{
????????//?Do?something
????}
????else?if?(str.Trim().StartsWith("Registry"))
????{
????????//?Do?something?else
????}
}
?
關(guān)于這個問題,我也請教了Kit George:
?
Kit,
Hi, I'm Allen Lee. I want to ask you in what situations we shall use System.Collection.Specialized.StringCollection? When I used Reflector to deassembly .NET, I found that StringCollection actually uses a ArrayList to provide its functions and it doesn't have a better performance than ArrayList! What's more, in .NET 2.0, we have generics; then is it necessary to use StringCollection or leave it alone in our future development?
I'm looking forward to hearing from you! Thanks!
Yours, sincerely
Allen Lee
Friday, March 18, 2005
?
?
Allen: leave it alone, and use List. StringCollection should probably be avoided now we have generics.
?
可見,在.NET 2.0中,泛型集合類應(yīng)該作為我們處理這類問題的首選工具。如果你對使用泛型集合類感興趣的話,可以看看《Power Collections - An Extension Library for System.Collections.Generic》這個MSDN TV。
?
總結(jié)
以上是生活随笔為你收集整理的StringCollection FAQ [C#, BCL]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ajax里的onreadystatech
- 下一篇: 《大数据》2015年第2期“研究”——特