c# - Defining my own Where-Method for LINQ to Objects - How do I know which one will get used? -


just testing reasons, defined own where-method linq so:

namespace test {     public static class linqtest     {         public static ienumerable<tsource> where<tsource>(                         ienumerable<tsource> source,                         func<tsource, bool> predicate)         {             return new list<tsource> { };         }     } } 

so if use query this, never result:

    var test = new string[]{ "a", "b", "c" };     var x = y in test             y.length > 0             select y;     foreach (var element in x)         console.writeline(element); 

my question is: how compiler know extension method supposed called? 1 included in linq, or user-defined one?

cheers, chris

the rules extension method similar normal method call lookup. short version compiler find of methods , extension methods of name accessible , choose best match among (with non-extension methods being preferred on extension methods).

just normal methods though choice of extension method use can ambiguous. case here if both system.linq , test namespaces used within application. based on information sounds test namespace not referenced using hence won't considered , linq version wins out.

note: above summary of how lookup occurs , no means definitive. c# language spec authority here , covers in greater detail.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -