Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Edit: I'm not sure that you can complete this as you're attempting. I've tried all sorts of tricks to attempt to get this to work and can't get it to compile. The best I can do is to pull the method call outside of my Generic class. If your method call is outside, then you can specifically define what T is in the generic. However, inside the method, at compile time, the compiler doesn't know what T will be so it doesn't know which overloaded method to call. The only way I can see around this is to use a switch to determine the type of T and manually specify the overload to call.</p> <p>The best I can do is this, which isn't quite what you're after, but it could be used to a similar effect:</p> <pre><code>class Stuff&lt;T&gt; { public T value { get; set; } } class Program { static void DummyFunc(Stuff&lt;int&gt; inst) { Console.WriteLine("Stuff&lt;int&gt;: {0}", inst.value.ToString()); } static void DummyFunc(Stuff&lt;string&gt; inst) { Console.WriteLine("Stuff&lt;string&gt;: {0}", inst.value); } static void DummyFunc(int value) { Console.WriteLine("int: {0}", value.ToString()); } static void DummyFunc(string value) { Console.WriteLine("string: {0}", value); } static void Main(string[] args) { var a = new Stuff&lt;string&gt;(); a.value = "HelloWorld"; var b = new Stuff&lt;int&gt;(); b.value = 1; var c = "HelloWorld"; var d = 1; DummyFunc(a); DummyFunc(b); DummyFunc(c); DummyFunc(d); } } </code></pre> <p>and got output:</p> <pre><code>Stuff&lt;string&gt;: HelloWorld Stuff&lt;int&gt;: 1 string: HelloWorld int: 1 </code></pre> <p>I've got four overloaded functions referencing two referencing generic classes (one for int and one for string) and two referencing regular types (one for int and one for string) and it all works okay... is this what you're after?</p> <p>Edit: The problem doesn't seem to be with the calling of the overloaded methods, it has to do with your foreach which is trying to convert all items in the list to the same type as the first in order to reference the overloaded method. The first item that doesn't conform to that exact definition will cause your compile to fail.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload