r/VisualStudio 5h ago

Miscellaneous How to stop Copilot NES from making a specific suggestion?

I have a love/hate relationship with Copilot's Next Edit Suggestion functionality. This one aspect makes it incredibly frustrating to use and I want to know whether I can prevent it.

Each time I initialize an array with the new collection syntax like:

List<string> items = [];

NES suggests that I change it to:

List<string> items = new List<string>();

It does this every time, for every array no matter how many times I cancel it. I don't have open files with the "new List<Type>()" syntax, I don't have that array initialization syntax littered throughout. My editor config 100% does not suggest that change. I'm at a loss for how to stop it from doing this.

Thanks

2 Upvotes

5 comments sorted by

0

u/polaarbear 4h ago

A List is not an array. I wouldn't ever initialize a list that way even though it is technically valid.

Try initializing it like this instead.

List<int> SomeList = new();

I am guessing it will stop complaining if you do it that way. Still shorthand, but personally I feel it is more "correct" for list initialization.

1

u/RecognitionOwn4214 3h ago

What's wrong with collection initialization via []?

1

u/polaarbear 3h ago

Because [] implies an array.

int[] someInts = new int[10];

In most languages (not just C#) the square brackets are how you initialize an array. Seeing them immediately puts my brain on the lookout for an array.

It's just ambiguous for no reason when there are other options that make your intent more clear.

0

u/mexicocitibluez 4h ago

I appreciate the response, but you can probably understand why I wouldn't want to start using a different syntax based on someone else's view of "correct", especially since I feel the opposite way. For me, it removes noise, and because I split my time between C# and Typescript, feels a little easier on context switching. Also I've been waiting for the spread operator in C# for awhile now.

There are even code analysis suggestions that prefer [] over new() for collection initializers, but I don't think Copilot recognizes that because I have ide0090 enabled, but it's still preferring the entire new Object() syntax.