Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you run this simple program it explains how overloading works :). The answer is that the underlying type's method will be called. And it should work the same way for <code>IQueryable</code> and <code>IEnumerable</code>; that is, the original object's method will be called since even though we cast it something else the actual implementation is of the original type. But when there is an extension method the extension method for the object which is referenced is used, so in that case it depends on the logic in the extension method. The extension method could have come clever logit to see if the object is of an actual type and then cast it back before it executes the method.</p> <pre><code>class Program { static void Main(string[] args) { MyString2 myString2 = new MyString2(); var myString = (MyString)myString2; Console.WriteLine(myString); // prints "ToString of MyString2" Console.WriteLine(myString.GiveMeTheString()); // prints "GiveMeTheString of MyString2" Console.ReadLine(); } } public class MyString { public string MyProperty { get; set; } public override string ToString() { return "ToString of MyString"; } } public class MyString2 : MyString { public string MyProperty { get; set; } public override string ToString() { return "ToString of MyString2"; } } public static class Extensions { public static string GiveMeTheString(this MyString myString) { return "GiveMeTheString of MyString"; } public static string GiveMeTheString(this MyString2 myString) { return "GiveMeTheString of MyString2"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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