Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Extension Methods Be Called From The Immediate Window

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.

like image 863
Ɖiamond ǤeezeƦ Avatar asked Jan 13 '12 12:01

Ɖiamond ǤeezeƦ


People also ask

How do you call an extension method?

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.

Where do you put extension methods?

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.

What is the purpose of immediate window?

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.

What are extension methods and where are they defined?

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.


2 Answers

Extension methods are just static methods.

You should be able to use e.g. System.Linq.Enumerable.ToList()

like image 165
Joe Avatar answered Sep 22 '22 09:09

Joe


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.

like image 36
doogle Avatar answered Sep 23 '22 09:09

doogle