Extension Methods not Recognized

ChiliYago picture ChiliYago · Apr 7, 2010 · Viewed 37k times · Source

What is necessary to have an extension method honored when it exists in an imported assembly? I built one in a class library project but it is not recognized in my web project which references the library. All the other classes and methods in the library are honored and visible but this extension method is not. The extension method is visible when used within the library.

Answer

dtb picture dtb · Apr 7, 2010

Referencing an assembly containing a class with extension methods is not enough. You need to import the namespace containing the class in each of your source file where you want to use the extension methods.

For example, to use LINQ-to-objects, you need to reference the System.Core assembly and import the System.Linq namespace (which contains the Enumerable class with the LINQ extension methods):

using System.Linq;