This is one of those little things I'm looking forward to getting out of C# 3.0: inferred types. I've always throught to myself, "I just declared the variable to be that type, can't you figure it out without me spelling it out for you, C# compiler?" Well, the compiler can't figure it out yet, but here's a little snippet I cooked up so at least I don't have to type the type name twice:
<?xml version="1.0" encoding="utf-8" ?><CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>var</Title> <Shortcut>var</Shortcut> <Description>Code snippet for declaring and initializing a variable with a type cast</Description> <Author>Adam Anderson</Author> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>type</ID> <ToolTip>Variable type</ToolTip> <Default>Type</Default> </Literal> <Literal> <ID>name</ID> <ToolTip>Variable name</ToolTip> <Default>Name</Default> </Literal> <Object> <ID>value</ID> <ToolTip>Initial value</ToolTip> <Default>Value</Default> <Type>System.Object</Type> </Object> </Declarations> <Code Language="csharp"> <![CDATA[$type$ $name$ = ($type$) $value$; $end$]]> </Code> </Snippet> </CodeSnippet></CodeSnippets>
To install, copy and paste the above XML into a file and save it with a .snippet extension to your code snippets directory (My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets). To use, simply type "var" and press Tab.
Now as you type the type name, it will be duplicated for you in the type cast.
Every little annoyance eliminated is productivity gained. Enjoy!
Remember Me
a@href@title, i, strike, u
Copyright © 2003-2008 Falafel Software Inc.
Subscribe to Falafel Blogs
The opinions expressed herein are Falafel's employees own personal opinions and do not represent Falafel Software's view in any way in case they go bananas!