Why does a String Array's "Count" Intellisensed property morph into "Length"?

I've got this code to see how many distinct fields/columns are being returned from a SQL Server Stored Proc (dtPriceComplianceResults is a DataTable that has been populated with the results of the SP):

string[] columnNames = dtPriceComplianceResults.Columns.Cast<DataColumn>()
                .Select(x => x.ColumnName)
                .ToArray();

When I add this code directly below:

if (columnNames.

...Intellisense gives me a "Count" option; when I select the Enter key to accept it, though, it morphs it into "Length" - it won't let me choose "Count" even though that is in the list of available properties to access.

Note: By "morphed" I mean when I hit "Enter" the text that is appended to "columnNames." is "Length" not "Count", so that it ends up as "columnNames.Length"

It does compile that way (using "Length"), and with "Count" it does not ("Operator '>' cannot be applied to operands of type 'method group' and 'int'").

I guess this is a good thing, but if "Count" is not going to be really selectable, why does it show up in the list?

Upvotes: 1

Views: 167

Answers (2)

CodeCaster
CodeCaster

Reputation: 151604

It's a ReSharper 10 "typo protection" option:

Correcting Length/Count mistyping

ReSharper prevents you from stumbling over mistyped Length/Count properties of arrays/collections. As soon as you erroneously start typing the Count property for a usage of an array, ReSharper will allow you to pick it from the completion list and replace with the Length property, which you probably meant to type.

You can see in the member info popup (the tooltip to the right of the IntelliSense dropdown) that it'll be replaced by System.Array.Length.

If you want the Count() extension method instead, you'll have to select the Count option prepended with the pink-with-arrow extension method icon, not the top one with the grey autocomplete icon:

enter image description here

Upvotes: 4

Henk Holterman
Henk Holterman

Reputation: 273274

Count() is a method, not a property. That's why you get the "cannot be applied to operands of type 'method group' ..." error.

It is an extension method for IEnumerable and the implementation will (eventually) return Array.Length.

You could use it but it's less efficient than the simple (and direct) Length.

Intellisense or Resharper is doing the right thing.

Upvotes: 2

Related Questions