I would like to automatically transfer the value to the general container when returning (I know that this is not always desirable, but it makes sense for my case). For example, I would like to write:
public static Wrapper<string> Load() { return ""; }
I can do this by adding the following to the Wrapper class:
public static implicit operator Wrapper<T>(T val) { return new Wrapper<T>(val); }
Unfortunately, this fails when I try to convert IEnumerable , the full code here (and in ideone ):
public class Test { public static void Main() { string x = ""; Wrapper<string> xx = x; string[] y = new[] { "" }; Wrapper<string[]> yy = y; IEnumerable<string> z = new[] { "" }; Wrapper<IEnumerable<string>> zz = z;
Compilation Error:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to '...Wrapper<System.Collections.Generic.IEnumerable<string>>'
What exactly is happening, and how can I fix it?
source share