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.
source share