Can visual studio offer variable names?

Eclipse can automatically create (or suggest) variable names. For example, if I have a class called MyClass, and I want to create an object from it, MyClass myclass = new MyClass (); In Eclipse, after the class name (MyClass), I press Ctrl + space, and Eclipse automatically suggests the variable name (myClass). Is there a similar feature in Visual Studio?

+4
source share
3 answers

Visual Studio does not do what you describe, but this functionality can be obtained through extensions. I know ReSharper does this (and much more), so you can try it out.

+2
source

There are two free options:

1) Free extension Visual Studio .NET AutoCode .
Just install it and enter the code:
MyClass i
and press Ctrl + Enter. AutoCode will replace MyClass i with MyClass myClass = new MyClass();

2) You can write a piece of code and save it, say, the name newvar.snippet, in the user snippets folder (usually this is 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 enter the code
newvar
and then press the Tab key twice

VS will insert type type = new type();
with the first "type" of the word highlited. Enter the appropriate class name MyClass (auto-complete enabled) and press Enter twice. VS will replace the code:
MyClass myClass = new MyClass();
Unfortunately, the variable would get the name of its class (see the SO answer and the proposal for visualstudio.uservoice.com ). But it is very easy to fix (wrong case of a variable) manually.

+1
source

Visual Studio does not offer variable names, but it should do most other things.

For example, if you do MyClass myClass = , then the IntelliSense window should appear in it with new MyClass(); .

However, it does have some useful things 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.

0
source

All Articles