.NET interview questions: - Steps to add a code snippet in Visual Studio 2010?
- By Shiv Prasad Koirala in ASP.NET
- Sep 26th, 2011
- 4408
- 0
Visual Studio has two types of snippets namely Expansion snippet which are inserted at the cursor and SurroundsWith snippet which wraps around existing code.
Let's see a small and simple example to understand it in better manner.
In order to see it practically just follow the following steps.
Step1: - create a new project of Console Application for that just go to >> File >> New >> Project >> Windows >> Select Console Application.
Step2: - Add XML file in your project for that just go to >> Solution Explorer >> Right Click on the project name >> Add >> Add New Item >> Select XML File.
Note: - Please save the XML File with .snippet extension like below diagram.
Step3: - Now just right click on code editor window and select the menu item Insert snippet or Press Ctrl + K + X and click the itemSnippet like below diagrams.
As soon as you select Snippet the following tags will be automatically to the code editor window like below diagram.
Step4: - Just remove the tag below as we are creating an Expansion snippet.
<SnippetType>SurroundsWith</SnippetType> Now simply just make the necessary changes to the tags as I have done in below code snippet. <?xml version="1.0" encoding="utf-8" ?> <CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <Header> <Title>CodeSnippetExample</Title> <Author>Feroz Shaikh</Author> <Shortcut>CodeSnippet</Shortcut> <Description>Add a Header to Code File</Description> <SnippetTypes> <!--<SnippetType>SurroundsWith</SnippetType>--> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>Author</ID> <Default>Feroz Shaikh</Default> </Literal> </Declarations> <Code Language="CSharp"> <![CDATA[ /**************** Code File Header ****************** Author: $Author$ Date: 26/09/2011 Version: 1.0.0.1 Description: Adding a header to the Code File ****************************************************/ ]]> </Code> </Snippet> </CodeSnippet>
Step5: - Now we have to load the snippet in the Visual Studio for that we have to use Code Snippet Manager which comes underTools menu like below diagrams.
Go to >> Tools >> Customize >> Commands >> Tools >>Add Command >>Select Code Snippet Manager.
Step6: - Now simply just save the file (MyCodeSnippet.snippet) to your desired location like below diagram.
Step7: - Now just simply open the code snippet manager and click on Import like below diagram.
Step8: - Now just simply open the program.cs file of console application and just add the created snippet in the code like below diagram.
Just right click on the code to add the snippet.
Now you will see that snippet is now being added to the code like below diagram.
Similarly, you can test for the second method SurroundsWith.
Also view our video on CAS, evidence, permission set & code groups as follows: -
Avail from the link more important .NET interview questions for preparation.
Regards,
Refer author's other blog for complete Most asked .NET interview questions
Shiv Prasad Koirala
Visit us @ www.questpond.com or call us at 022-66752917... read more