I have a very simple recursively defined function that prints out the contents of any list, defined as such;
static string ShowList<T>(IEnumerable<T> iterable)
{
return "[" + string.Join(", ", iterable.Select(e => ShowList(e))) + "]";
}
static string ShowList(string str)
{
return $"\"${str}\"";
}
static string ShowList<T>(T elem)
{
return elem.ToString();
}
As you can see, the lowest override is a catch all which applies to any argument. The highest overload only applies to arguments which are enumerables. Ideally I want it to check against and run the most specific overloads first, and then if they all fail, use the general case. But what I find is that it always goes straight for the general case immediately, and just prints out Systems.Generic.Containers.List1[]
. Is it possible to give certain overloads more precedence than others, so that the compiler will automatically try those before others?
>`
– Maurdekye Nov 17 '16 at 18:49>`, it doesn't apply it to the first overload, it applies the whole thing to the third. What I want it to do is apply it to the first, where then each element (a `List`) is applied to the first again, and each element from there (`string`) is applied to the second overload.
– Maurdekye Nov 17 '16 at 18:52