I'm trying to understand why implicit conversion in the following code causes StackOverflowException
. I think it is a covariance/contravariance issue, but I cannot explain why at the moment.
Crashes NUnit:
private List<T[]> _NodesContent = new List<T[]>();
private UnrolledLinkedListBuilder<T> AddNodes(IEnumerable<T[]> nodes)
{
_NodesContent.AddRange(nodes);
return this;
}
public UnrolledLinkedListBuilder<T> AddNodes(params T[][] nodes)
{
return AddNodes(nodes);
}
Works:
private List<T[]> _NodesContent = new List<T[]>();
private UnrolledLinkedListBuilder<T> AddNodes(IEnumerable<T[]> nodes)
{
_NodesContent.AddRange(nodes);
return this;
}
public UnrolledLinkedListBuilder<T> AddNodes(params T[][] nodes)
{
return AddNodes((IEnumerable<T[])nodes);
}
As I understand from this answer https://stackoverflow.com/a/275107/761755 conversion should be performed implicitly.
Moreover, if in the first example you directly call _NodesContent.AddRange(nodes)
from AddNodes(T[][])
there will be no exception.