Intellisense lists many unnecessary <Extention> methods for objects of type String?

32 Views Asked by At

In my VB.Net 2019, the Intellisense auto-complete list is showing many unnecessary methods for objects of type String.

These methods all have the <Extension> attribute, as shown in the attached screenshot. Many of them seem to be data-related, such as Any, All, and Distinct. But others are not and seem wrong for String, such as Average, Count, and Intersect.

For this screenshot, I created a test-project, and removed everything from References except System and Microsoft.VisualBasic, and still the extension methods are there.

Where do these methods come from?

I understand I can click the buttons on the auto-complete list for Properties and Methods to show only those. But also I would like to understand where the extension methods are coming from for my own education.

enter image description here

2

There are 2 best solutions below

3
Craig On

You're seeing those extension methods because String implements IEnumerable(Of Char) and the System.Linq namespace is automatically imported as part of your project properties. The methods you highlighted are standard Linq extension methods for anything that implements IEnumerable(Of T).

You can see this if you look closely at the IntelliSense; note that it reads "<Extension> Function IEnumerable(Of Char).Aggregate...".

You can control the automatic imports through the "References" ply in project properties (it's in the same place in both original and SDK style projects). I believe System.Linq is one of the defaults.

Additionally, there are options settings where IntelliSense may "show items from unimported namespaces" (see Tools / Options / Text Editor) which may lead to the extension methods being shown even if you have removed System.Linq as a default import. See the self-answer for more details on changing these option settings.

1
spinjector On

Thanks to @Craig's answer, I figured it out.

He mentioned Linq, so I searched and found this post about Linq in C#. It gave me some ideas, and after an hour of clicking around my systems to test & verify, I solved the issue.

I verified this on two different systems with Visual Studio 2019, and it works for projects in VB and C#.

The solution is as follows:

  1. Click Tools > Options > Text Editor > Basic (or C#) (or both) > Intellisense.
  2. If checked, uncheck the last option Show items from unimported namespaces (experimental).
  3. In the Project Properties, on the lower half of the window, uncheck System.Linq. Or for C#, remove the declaration using System.Linq.
  4. The key and final step is to close/save/reopen the solution/project, and then the <Extension> methods will be gone. In fact, even that button will disappear from the Intellisense auto-complete list, as shown in the screenshot below.
  5. Unfortunately, System.Linq will need to be unchecked/removed for every new project. Then close/reopen and the Extension methods will again be gone.
  6. I could not find a way to make it go away permanently, even for new projects.

enter image description here