Eclipse can automatically create (or suggest) variable names. For instance if I have a class named MyClass and i want to create an object from it, MyClass myClass = new MyClass(); In Eclipse after class name (MyClass) i hit ctrl+space key and Eclipse automaticly suggests name for the variable (myClass). Is there any similar feature in Visual Studio?
3 Answers
Visual Studio does not do what you're describing, but it is possible to get this functionality through extensions. I know ReSharper does this (and much more), so you might want to give it a try.
There are two free options I see:
1) Free Visual Studio extension .net AutoCode.
Just install it and type in the code:MyClass i
and press Ctrl + Enter. AutoCode will replace MyClass i
withMyClass myClass = new MyClass();
2) You could write a code snippet and save it with, say newvar.snippet name, to custom snippets folder (usually C:\Users\UserName\Documents\Visual Studio NNNN\Code Snippets\Visual C#\My Code Snippets):
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>newvar</Title>
<Author>
</Author>
<Description>Declares varible with the same name as its type name.</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>newvar</Shortcut>
</Header>
<Snippet>
<Imports>
<Import>
</Import>
</Imports>
<Declarations>
<Literal Editable="true">
<ID>type</ID>
<ToolTip>type</ToolTip>
<Default>type</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[$type$ $type$ = new $type$($end$);]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
When you restart your VS and type in the codenewvar
and then press Tab key twice
VS will insert
type type = new type();
with first "type" word highlited. Type in appropriate class name MyClass
(autocompletion is enabled) and press Enter key twice. VS will replace code:MyClass MyClass = new MyClass();
Unfortunatly, variable would get the name of its class (see SO answer and suggestion on visualstudio.uservoice.com).
But it's very easy to fix (improper variable case) manually.
Visual Studio does not suggest variable names, but it should complete most other things.
For example, if you do MyClass myClass =
it should pop up an IntelliSense box with new MyClass();
in it.
It does have some useful things, however, that can quickly create things for you. For example, if you type class
and press TAB twice, it will create a class template, or if you type ctor
or mbox
it will create a constructor or MessageBox.Show()
call, respectively.