Stringcollection FAQ [C #, Bcl]

Source: Internet
Author: User
Tags reflector

Stringcollection FAQ [C #, Bcl]

 

Updated on Monday, March 21,200 5

 

Written by Allen Lee

Q: How to Use stringcollection [1]?

A: There are usually three methods to access the string element in stringcollection:

// 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: What are the advantages of stringcollection over arraylist?

A: First, let's take a look at the following:Code:

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

From the code above, we can see that the code written using stringcollection is more intuitive (especially when you want to perform complex operations on the string element in the set ), clearly shows the nature of the elements in the Set, and avoids manual forced type conversion.

Q: Is stringcollection specially optimized for string?

A: No! This may be the biggest misunderstanding. Don't see that the stringcollection is only used to store strings, so it is specially optimized for storing strings!

Although code written with stringcollection is more intuitive than arraylist to some extent, the latter is a little better than [2] in terms of performance. As a matter of fact, stringcollection only mounts the arraylist (The following is the stringcollection code snippet decompiled with reflector ):

// Code #03

[Serializable]
Public   Class Stringcollection: ilist, icollection, ienumerable
{
// Methods
Public   Void Addrange ( String [] Value)
{
If (Value =   Null )
{
Throw NewArgumentnullexception ("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;
}

From the code, we can see that the stringcollection actually redirects the functions it provides to arraylist. Their relationship is use-.

Q: under what circumstances should we use stringcollection?

A: There is no standard answer to this question, depending on the problem you want to solve and your handling principles. First, let's hear Robert B. Murray's thoughts:

Program The correctness is more important than its speed. Compared with human resources, the computer's running time overhead will become cheaper and cheaper. For methods that increase performance and cause program comprehensibility and maintainability to deteriorate, we should be cautious. [3]

Therefore, using stringcollection instead of arraylist has a certain effect. But personally, I am used to using arraylist. Of course, if you use. NET 2.0, then list It will be your best choice. It combines the advantages of stringcollection and arraylist-intuitive code and excellent performance!

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

For this question, I also consulted 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,200 5

ALLEN: Leave it alone, and use list . Stringcollection shocould probably be avoided now we have generics.

It can be seen that in. NET 2.0, the generic collection class should be our preferred tool to deal with such problems. If you are interested in using generic collection classes, you can check the msdn TV "Power collections-an extension library for system. Collections. Generic.

 

    • [1] The Collection class is located in the system. Collections. Specialized namespace.
    • [2] for Performance Comparison Between stringcollection and arraylist, refer to the aierong article.
    • [3] [us] by Robert B. Murray; translated by Wang Xin; C ++ programming idioms-common methods and skills for senior programmers; China Power Press, 2004

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.