.NET interview questions: – Various steps to add a code snippet in Visual Studio 2010?

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 item
Snippet 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 under
Tools 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 Dotnet interview questions and answers for preparation.

Regards,

Refer author’s other blog for complete Most asked Dotnet interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s