Reputation: 3834
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?
Upvotes: 3
Views: 205
Reputation: 5305
As you mentioned they are both Collections, that means, they both implemented ICollection
, but like inheritance, the most specific objects are different things but have some characteristics that are the same. For example, suppose that we have tow classes:
class Plant : ILivingOrganism {...}
class Human: ILivingOrganism {...}
they have both implemented ILivingOrganism
interface, that they have some characteristics which are the same, like void Grow()
behavior which means they both can grow because they are living organism, but the thing is they are two different things even though they share some characteristics which are the same. Now when I write a method that returns Plant
obviously you cannot expect it to return a Human
, but with a method that returns ILivingOrganism
, like ILivingOrganism Clone()
, you can expect any living organism (more general type), like Human
or Plant
, technically everything that implements ILivingOrganism
.
Upvotes: 1
Reputation: 125630
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 string
s, 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.
Upvotes: 6
Reputation: 7457
It is invalid to return the list directly because the specified return type is an array
↓
public string[] GetStringList(params object[] values)
If you want to return a list instead, it should be
public List<string> GetStringList(params object[] values)
If you want both to be valid, you could make the method return an IEnumerable
(or another interface both types implement, like ICollection
) like this:
public IEnumerable<string> GetStringList(params object[] values)
Upvotes: 4
Reputation: 3965
Your function return string[]
instead of List<string>
that's as you want.
public List<string> GetStringList(params object[] values)
{
List<string> _stringList = new List<string>();
foreach (object value in values)
{
_stringList.Add(Convert.ToString(value));
//Do something
}
//Why do required to convert list collection in array
return _stringList;
}
Upvotes: 1
Reputation: 148120
Array
and List
are two different things, Dictionary
is also a collection then why dictionary and array are the same?. There are so many types of collections and they all are different from each other. If you do not want to convert then you shall return list instead of Array.
public List<string> GetStringList(params object[] values)
{
List<string> _stringList = new List<string>();
foreach (object value in values)
{
_stringList.Add(Convert.ToString(value));
//Do something
}
return _stringList;
}
Upvotes: 3