chiname

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

StringCollection FAQ

StringCollection FAQ [C#, BCL]

StringCollection FAQ [C#, BCL]

 

Writen by Allen Lee

Q:如何使用StringCollection[1]

A:通常我们有三种方法来访问StringCollection里面的string元素:

// Code #01

StringCollection 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有什么优势?

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元素进行复杂的操作的时候),清楚地表明了集合里面的元素的性质,并且免除我们手动进行强类型转换。

Q:StringCollection是否为string作了专门的优化?

A:没有!这可能是一个最大的误解,别看到StringCollection只用来储存string就以为它为string的储存作了专门的优化!

虽然在某程度上用StringCollection写的代码比ArrayList的更加直观,然而后者却在性能上稍胜一筹[2]。事实上,StringCollection只是对ArrayList进行了一番装裱而已(以下是使用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 (stringthis.data[index]; }
        
set this.data[index] = value; }
    }


    
// Other properties omitted

    
// Fields
    private ArrayList data;
}

从代码中我们可以看到StringCollection实质上把其所提供的功能重定向给ArrayList,它们的关系是Use-A。

Q:在什么情况下我们应该使用StringCollection呢?

A:这个问题没有标准答案,视你所要处理的问题和你的处理原则而定。就我个人来说,我更倾向于使用ArrayList。当然,如果你使用的是.NET 2.0,那么List<T>会是你不二的选择,它结合了StringCollection和ArrayList的优点——代码直观和性能优良!

// Code #04

List
<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
    }

}

关于这个问题,我也请教了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 you! Thanks!

Yours, sincerely
Allen Lee
Friday, March 18, 2005

Allen: leave it alone, and use List<T>. StringCollection should probably be avoided now we have generics.

可见,在.NET 2.0中,泛型集合类应该作为我们处理这类问题的首选工具。如果你对使用泛型集合类感兴趣的话,可以看看《Power Collections - An Extension Library for System.Collections.Generic》这个MSDN TV。

 


  • [1] 该集合类位于System.Collections.Specialized命名空间。
  • [2] 关于StringCollection和ArrayList的性能比较,可以参考aierong的文章

 

posted on 2005-03-19 12:31 Allen Lee 阅读(196) 评论(2)  编辑 收藏

评论

# re: StringCollection FAQ 2005-03-19 13:27 montaque

呵呵,如果要你去优化这个stringcollection 你会怎么处理?

效率一定高很多吗?   

# re: StringCollection FAQ 2005-03-19 14:07 aierong


从Reflector反编译的代码得知

StringCollection就是利用一个私有字段data来存放数据的
刚好这个字段是ArrayList类型的


从索引器可以看到,StringCollection帮我们做了类型转化
从object转为string



这样看来,2者性能不会有什么太大差异

我明白了

当集合类仅仅存放不定数量字符时我还是选择StringCollection
这样代码清晰些

 re: StringCollection FAQ
posted on 2005-03-20 11:54  把我的欢乐带给你  阅读(415)  评论(0)    收藏  举报