Why should I convert List into Array

Ankush Madankar

Look into following line of code:

public string[] GetStringList(params object[] values)
{
    List<string> _stringList = new List<string>();

    foreach (object value in values)
    {
        _stringList.Add(Convert.ToString(value));

        //Do something
    }

    //Why this invalid?
    return _stringList;

    //Why do required to convert list collection in array
    return _stringList.ToArray();
}

Since List and Array both are colletions then why should I need to convert List into Array?

MarcinJuraszek

Since List and Array both are collections then why should I need to convert List into Array?

Yes, they are both collections, but your method signature specifies exactly what type of collection it returns, and that's why you have to return exactly that one collection type.

You can change your method declaration to return collection of strings using one of collection interfaces:

public IEnumerable<string> GetStringList(params object[] values)

or

public ICollection<string> GetStringList(params object[] values)

It will allow you to return both List<string> and string[], because they both implement the interface. You could also return HashSet<string> or even Queue<string> (for IEnumerable<string> version only. Or write your own class, implement the interface you're interested in and return it within your method.

But as soon as you declare the method using string[], you have to return something that is an array of strings, and List<string> does not fulfill that requirement.

OFT advice

You can make your code a little faster passing values.Length as List<string> constructor parameter:

List<string> _stringList = new List<string>(values.Length);

it will initialize list internal storage with necessary amount of memory and let you add all your items without reallocating any memory.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How should I convert a singleton Array to a scalar?

Why convert an array to a list to an array again

List class's toArray in Java- Why can't I convert a list of "Integer" to an "Integer" array?

what does python "array" module do and why should i use it instead of a list?

Which array/list should I use?

Why would I want to convert an array into an object

Why should I convert iterable object to iterator first?

How I convert string response into Array List

How i convert array to adjacency list of tree?

Can i Convert this array to List or Set in Typescript?

I need to convert a javascript array to a sql list

I want to convert an python output list to an array

How do I convert an array or a list into a hashref?

Should I iterate a Java collection to get a subset, or should I convert it to array first then iterate to get it?

Why I cannot convert this list as a string to be a real list in Common Lisp?

Why should I embed navigation bar links into a list and then into a navigation bar?

Why can I not add this object to array list?

Why should I declare a String as a fixed-size array

Why should I "Transform myVariable to final one element array"?

(Why) should I test if an array is empty prior to a for loop?

Why is ctypes so slow to convert a Python list to a C array?

How should I properly convert mp3 to byte array and then convert it back to mp3

Why can I list an array with List, but not with a For Loop and Text?

Python: Why I can not convert map object to list

I am trying to fetch data from firestore and then convert the data into an array. the new array should be an observable

Why is my output empty when I convert a set into an array?

How I can convert an array value from LInkedHashMap to the List?

How do I convert java.sql.Array to List<MyCustomEnum>?

How do I convert an array list to hash separated string?