In the below code,
is it more efficient (in terms of memory and time) to create a List<string>
directly, rather than creating a string[]
and calling ToList()
on it before passing it to SomeMethod()
?
If you create the string[]
and call ToList()
, would it create a List<string>
object, as well as already having the string[]
?
The reason I ask is that I have seen code where they are creating a string[]
and calling ToList()
before passing it as an argument to a method with a List<string>
parameter, and I wasn't sure if there was any particular reason for that.
class Program
{
static void Main(string[] args)
{
var array = new string[] { "str1", "str2" };
SomeMethod(array.ToList());
var list = new List<string> { "str1", "str2" };
SomeMethod(list);
}
static void SomeMethod(List<string> list)
{
//do stuff
}
}