Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
String collection versus string array
Message
De
18/08/2009 08:48:14
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
18/08/2009 08:22:22
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01418535
Message ID:
01418633
Vues:
36
>>>>>>Hi all,
>>>>>>
>>>>>>I was just pondering the use of List collection versus a string array when used as a simple collection of string items. For instance how would the reliability of problems be if I did this:
>>>>>>
>>>>>>List<string> myList = new List<string>
>>>>>>myList.Add("One");
>>>>>>myList.Add("Two");
>>>>>>etc..
>>>>>>
>>>>>>if (myList.Count > 0)
>>>>>> int mycount = myList.Count;
>>>>>>
>>>>>>versus
>>>>>>
>>>>>>string[] myStrings = new string[];
>>>>>>myStrings[0] = "One";
>>>>>>myString[1] = "Two";
>>>>>>etc;
>>>>>>
>>>>>>if (myString.Length > 0)
>>>>>>  int mycount = myString.Length;
>>>>>>
>>>>>>
>>>>>>The reason for my question is I have some methods that expect a string array to be passed in. I would much rather create my arguments out of collections but I would have to convert them all some how or re-write the methods that use a string array.
>>>>>>
>>>>>>By the way, what is the best way to convert from a collection to an array - both of strings of course?
>>>>>>
>>>>>>Tim
>>>>>
>>>>>Tim,
>>>>>
>>>>>You could have your methods to expect/accept an IEnumerable(String). That way they would accept both arrays and collections
>>>>>
>>>>>No need to convert anything - at all
>>>>>
>>>>>ps: the IEnumerable(T).Count is slower since it has to traverse/enumerate the whole enumerator
>>>>
>>>>Oh, now that is a great idea. Thanks. The part of the question I only eluded to was this. Is there a clear definition somewhere that would indicate when you should use which type?
>>>>Thanks again
>>>>Tim
>>>
>>>hi Tim,
>>>
>>>Only my 2 cents ...
>>>
>>>
MyMethod(IEnumerable<String> items)
works with an interface.
>>>It provides more flexibility in that it accepts arrays, lists and anything that implements IEnumerable(String). You need only MyMethod with one signature vs a signature per type (array, list, ..)
>>>
>>>If you look at the constructor of the collectiions (list/queue/stack/.. ) they all have a constructor that accepts an IEnumerable(T) to initalize
>>
>>Thanks Gregory,
>>
>>I am goin to change it over so it is more flexible. The existing methods weren't my code so I was trying to implement base class methods that ultimately use them and they were all arrays. I think I got this figured out now and thanks for your help along with others.
>>Tim
>
>
>Perhaps the code was written in C# 1 : arrays + arrayLists

Not sure but perhaps. Not saying it was bad, just wasn't my idea of how to do it.
Tim
Timothy Bryan
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform