The following code doesn't compile:
public void CreateStringList(out List<string> newList)
{
newList = new List<string>();
}
...
IEnumerable<string> myList;
CreateStringList(out myList);
The error given is:
The out parameter type doesn't match the parameter type
My question is... why doesn't this work? IEnumerable<string>
is covariant with List<string>
, so the assignment will never violate type-safety. And you're not allowed to use an out
parameter before assigning it, so the fact that the previous value of newList
might not have been a List<string>
is irrelevant.
Am I missing something?