views:

126

answers:

3

In Visual Studio Professional 2010 whenever I type the following:

new {

It automatically changes to:

new object {

Is there a way to make it not do this? "Object" does not have the properties of the object I want to anonymously create.

+4  A: 

https://connect.microsoft.com/VisualStudio/feedback/details/584429/autocomplete-on-new-is-interpreted-as-a-new-function-instead-of-anonymous-class

I'm pretty sure it's a bug, so I went ahead and reported it. Was going to do it sooner or later anyway :)

Hope that's okay with you.

Rei Miyasaka
I have always assumed it is a bug as well.
thekaido
Strange, I thought it was something stupid I had to enable -- like line numbers. Speaking of line numbers, why are those disabled by default?
npsken
@Rei the second case you described (new Func) is correct behavior... you've not declared the input parameters to the lambda, so intellisense is still in "locate delegate" mode, not "compose lambda body" mode.
Rex M
@Rex Can you give me an example of what you mean by "locate delegate"?
Rei Miyasaka
@Rei you have chosen the `Select` method which takes a `Func<T,TResult>`. It doesn't have to take a lambda, it could also take a real delegate that already exists or a new `Func<T,TResult>` delegate to an existing method. You haven't typed `foo =>`, so it doesn't know you want a lambda, so it is trying to autocomplete with whatever matches, which is a new delegate. If you typed `foo =>` instead, it would know you want a lambda and you're now inside the lambda body, it would not try to give you a delegate because that's no longer the most likely match when you type `new`.
Rex M
I think my mind's going. I've been using C# since version 1.1 and C# 3 since version... C# 3, but for some reason I'm lost hah. Shouldn't it be expecting an argument list first anyway? It's not too important but I'm probably mistaken. Hopefully it doesn't affect their decision.
Rei Miyasaka
+3  A: 

You can disable the IntelliSense completing when you type the bracket.

On the Tools menu select Options. Then, on the right hand side, expand Text Editor then C# then IntelliSense. Remove the { from the textbox under the Committed by typing the following characters:

You may also have to uncheck the Committed by pressing the space bar or get in the habit of writing new{ and relying on the auto formatting when you close the bracket (though I've never done any ASPX stuff so don't know how good the auto formatting is compared to a normal code file.)

Sam
Does this affect any of the other autocomplete features?
Rei Miyasaka
It should work more or less the same, only you'll need to press tab instead of **{**. The **Committed by pressing the space bar** would be a pretty big upset for me so I'd leave that one checked, just included it in the answer for completeness.
Sam
A: 

I've just gotten in the habit of typing "new{}". Intellisense doesn't kick in then. And since I'm always reformatting the page anyways with crtl-k;crtl-d, it spaces it out correctly later on.

Benjamin Anderson