I ask the question because whenever I attempt to call an extension method from the Immediate window in Visual Studio 2010 I get the following error:
System.Collections.Generic.IEnumerable' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)
If the Immediate window doesn't support extension methods, then why is it that when I type my variable (of type IEnumerable<QueryFilter>
) followed by a dot, the IntelliSense lists all the extension methods?
There is nothing wrong with what I am typing in the Command window because if I copy and paste it into my code file and run, it works.
With Visual Studio 2012 doing the same thing for the same solution works fine. If I switch back to VS2010 and the problem persists.
To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.
An Extension Method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement. You can give any name of for the class that has an Extension Method but the class should be static.
Use the Immediate window to debug and evaluate expressions, execute statements, and print variable values. The Immediate window evaluates expressions by building and using the currently selected project.
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.
Extension methods are just static methods.
You should be able to use e.g. System.Linq.Enumerable.ToList()
The extension method translates to "Enumerable.ToList" The compiler would normally convert
myList.Tolist();
To:
Enumerable.ToList(myList);
during compile time. I believe you can use extension methods from the quickwatch window if you so wanted to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With