Thursday, October 8, 2015

Some useful c# code snippets for visual studio users

Public Class - code snippet

(Code snippet for public class)
Many times we, visual studio users, use common code snippets (already available with visual studio setup) like for, if, class, prop etc...
Out of these class is used many times, an every time we need to prepend Public with generated class declaration. So to automate this here I have created a custom C# code snippet that generates code for class declaration along with Public :

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
 <CodeSnippet Format="1.0.0">
  <Header>
   <Title>pclass</Title>
   <Shortcut>pclass</Shortcut>
   <Description>Code snippet for public class</Description>
   <Author>Microsoft Corporation</Author>
   <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
    <SnippetType>SurroundsWith</SnippetType>
   </SnippetTypes>
  </Header>
  <Snippet>
   <Declarations>
    <Literal>
     <ID>name</ID>
     <ToolTip>Class name</ToolTip>
     <Default>MyClass</Default>
    </Literal>
   </Declarations>
   <Code Language="csharp"><![CDATA[public class $name$
 {
  $selected$$end$
 }]]>
   </Code>
  </Snippet>
 </CodeSnippet>
</CodeSnippets>

Copy above code and save into a file with name - "pclass.snippet" at "%USERPROFILE%\Documents\My Projects\Code Snippets\Visual C#\My C# Code Snippets". After restarting visual studio in any C# class file, type pclass + tab and see the difference.

Property with matching name - code snippet

(Code snippet for property and backing field with matching name)
One more code snippet - propfull we use many times, an every time we need to change Property name and private variable name with generated class declaration, though we use the same name for both with prepending underscore(_). So to automate this here I have created a custom C# code snippet:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
 <CodeSnippet Format="1.0.0">
  <Header>
   <Title>propmatchname</Title>
   <Shortcut>propmatchname</Shortcut>
   <Description>Code snippet for property and backing field with matching name</Description>
   <Author>Microsoft Corporation</Author>
   <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
   </SnippetTypes>
  </Header>
  <Snippet>
   <Declarations>
    <Literal>
     <ID>type</ID>
     <ToolTip>Property type</ToolTip>
     <Default>int</Default>
    </Literal>
    <Literal>
     <ID>property</ID>
     <ToolTip>Property name</ToolTip>
     <Default>MyProperty</Default>
    </Literal>
   </Declarations>
   <Code Language="csharp"><![CDATA[private $type$ m_$property$;
 public $type$ $property$
 {
  get { return m_$property$;}
  set { m_$property$ = value;}
 }
 $end$]]>
   </Code>
  </Snippet>
 </CodeSnippet>
</CodeSnippets>

Copy above code and save into a file with name - "propmatchname.snippet" at "%USERPROFILE%\Documents\My Projects\Code Snippets\Visual C#\My C# Code Snippets". After restarting visual studio in any C# class file, type propmatchname + tab and see the difference. Now try to change either public property name OR private variable name. Both will be in sync.


Hope many visual studio users/developers love this.
Enjoy !!!

Post a Comment

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps