15

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.

Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40

5 Answers5

6

Extention methods are syntax sugar. Actually they are implemented static with the this keyword. You can call any extention method using the static method that provides the extention method. Then you should pass the object that is being extended as the first parameter.

Myrtle
  • 5,761
  • 33
  • 47
  • 1
    Why does IntelliSense list the extension methods, if I am unable to call them using that syntax? Is this a bug in Visual Studio? – Ɖiamond ǤeezeƦ Jan 13 '12 at 13:18
  • 1
    Voted down because it does not answer the question as to why extensions methods cannot be called using the normal fluent notation from within the immediate window. – bradgonesurfing Oct 10 '16 at 07:15
  • 1
    @bradgonesurfing thanks for explaining. However the primary question is 'can' and not 'why'. The answer is no, and I also provide a workaround. Unfortunately i cannot provide answer to the secondary why question. – Myrtle Oct 10 '16 at 09:13
  • 1
    while this is true, and I've been calling the methods directly from Enumerable, but this is extremely unwieldy. calling more than one linq extension method will end up being ungodly long and a lot of type in the immediate window. It's a bit frustrating. – stephenbayer Jan 09 '17 at 16:56
  • This is not helpful for the case where you are pasting real code from your program – StayOnTarget May 27 '22 at 12:30
5

It's because the System.Linq namespace is not imported in the current context you are while debugging.

Add

using System.Linq;

in your code.

Example with Visual Studio 2010:

enter image description here

First times with System.LINQ imported, then without using System.LINQ.

EDIT: If the namespace is imported and IntelliSense is displaying the methods, then it might be a bug of the Immediate window. See this bug entry on connect.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • @Aphelion you're wrong, Immediate window **definitely does support extension methods** (just tested VS2010). At least test it before voting down -__- – ken2k Jan 13 '12 at 13:38
  • @Ɖiamond ǤeezeƦ => What is the version of Visual Studio/C# you are using? – ken2k Jan 13 '12 at 13:48
  • @ken2k Visual Studio 2010. AlsocI have `using System.Linq` at the top of the file I am debugging. – Ɖiamond ǤeezeƦ Jan 13 '12 at 14:00
4

Extension methods are just static methods.

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

Joe
  • 122,218
  • 32
  • 205
  • 338
  • 2
    Voted down because it does not answer the question as to why extensions methods cannot be called using the normal fluent notation from within the immediate window. – bradgonesurfing Oct 10 '16 at 07:15
2

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.

doogle
  • 3,376
  • 18
  • 23
1

This behaviour is caused by Code Contracts, and is not limited to just the Immediate window but also the Conditional Breakpoints window too.

Update March 01, 2016: Found this MSDN Question asking why type resolution is not working in my watch windows. The behaviour described is exactly the same as I experience when using the Immediate Window. The cause is also attributed to CodeContracts and a bug report has been filed on Microsoft Connect. Whether or not the bug is resolve is not indicated.

Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40
  • 2
    Can you give some explanation why or how Code Contracts causes this problem? – gabe Jun 27 '12 at 13:57
  • No explanation. Tested it by setting up another machine, verified it worked correctly and then installed code contracts, resulting in the problem occurring. – Ɖiamond ǤeezeƦ Jun 28 '12 at 08:27
  • It's not just my set-up, the exact same thing is also happening to my colleague. Running the exact same solution in Visual Studio 2012 and issuing the commands in the Immediate Window work fine. Switch back to VS2010 and the problem persists. – Ɖiamond ǤeezeƦ May 09 '13 at 20:59
  • I don't have code contracts installed and see the same problem – bradgonesurfing Oct 10 '16 at 07:18