Introduction
What is a C# Code Snippet?
The code block, which is wrapped inside a CDATA section of a XML file and the Microsoft IDE provides a mechanism to explore this code block, which is written in the XML data to insert into a page we are working on. On a large project, this is common usage some senior level people does the common code block which reusable is shared across the project will make available in the Code Snippet form. While we create module you can put you patterns and practices in code snippets and do an easy development. Remember the code Snippet always gives a base template hides in XML CDATA Section Where As you need to change this according to your need.
Background
It is a developer's tendency to achieve functionality and forget the basic structure required in the project. Later we need to add region, try blocks and copyright information to make it professional. I myself have done many of these reworks, so my current project inspired me to think of different directions to implement an Auto Intelligence to avoid such situations.
Using the code
Example With Scenario.
I have created A Silverlight business application like this and added a Class file in the Project like this:
Step 1: Adding the Copyright InfoWhen I press 'co', I got my code snippet like this.
In addition, when I press the tab key 3 times I got all copyright information like this:
Step 2: Adding the Class Structure
I have revoked auto class template come then it look like this.
When I type "Simpleclass", then I press the tab key 3 times then I get all Class information like this:
Step 3: Adding the private and public class
Go to API Public Methods or Private Methods
The type "SimplePublicMethods" or "MySimplePrivateMethod" ", then I press the tab key 3 times and I get all Method information like this:
Finally, your page looks like this:#region CopyRightsAiropse//Comments
//Sonata
//Sonataian
//MM-DD-YY
//MM-DD-YY
//MM-DD-YY
//
What is a C# Code Snippet?
The code block, which is wrapped inside a CDATA section of a XML file and the Microsoft IDE provides a mechanism to explore this code block, which is written in the XML data to insert into a page we are working on. On a large project, this is common usage some senior level people does the common code block which reusable is shared across the project will make available in the Code Snippet form. While we create module you can put you patterns and practices in code snippets and do an easy development. Remember the code Snippet always gives a base template hides in XML CDATA Section Where As you need to change this according to your need.
Background
It is a developer's tendency to achieve functionality and forget the basic structure required in the project. Later we need to add region, try blocks and copyright information to make it professional. I myself have done many of these reworks, so my current project inspired me to think of different directions to implement an Auto Intelligence to avoid such situations.
Using the code
Example With Scenario.
I have created A Silverlight business application like this and added a Class file in the Project like this:
Step 1: Adding the Copyright InfoWhen I press 'co', I got my code snippet like this.
In addition, when I press the tab key 3 times I got all copyright information like this:
Step 2: Adding the Class Structure
I have revoked auto class template come then it look like this.
When I type "Simpleclass", then I press the tab key 3 times then I get all Class information like this:
Step 3: Adding the private and public class
Go to API Public Methods or Private Methods
The type "SimplePublicMethods" or "MySimplePrivateMethod" ", then I press the tab key 3 times and I get all Method information like this:
Finally, your page looks like this:#region CopyRightsAiropse//
//
//
//
//
//
//
#endregion
using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;
namespace BusinessApplication2
{
public class SimpleClass {
#region Consts
{
public class SimpleClass {
#region Consts
#endregion
#region Members
#endregion
#region Properties
#endregion
#region Ctor
#endregion
#region Methods
#region API Public Methods
#region SimplePublicMethods
///
///
/// My Simple Method Which created By Auto IntelliSense ///
/// /// ///
{
string Value = string.Empty;
try {
}
catch (Exception ex)
{
ErrorWindow.CreateNew(ex.Message);
}
return Value;
} #endregion
}
return Value;
} #endregion
#endregion #region Private Methods
#region MySimplePrivateMethod
///
///
/// My Simple Method Which created By Auto IntelliSense ///
/// /// ///
{
string Value = string.Empty;
try {
}
catch (Exception ex)
{
catch (Exception ex)
{
ErrorWindow.CreateNew(ex.Message);
}
return Value;
} #endregion
}
return Value;
} #endregion
#endregion #endregion }
}
I hardly took a few seconds to complete this much of code; this is the beauty of code snippets.
How we can create code snippets and integrate to IDE
Creation
Step 1: Please go to the you "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#" if you are using windows 7 other wise search for "\Snippets\1033" or "\1033" under you VS root directory installation.
Step 2: Once you reach there you will get all code snippet file like this.
Step 3: Please take one code snippet and copy to your desktop and Edit that
CDATA SECTION with the code block you want to see when the code injects.public class SimpleClass{
I hardly took a few seconds to complete this much of code; this is the beauty of code snippets.
How we can create code snippets and integrate to IDE
Creation
Step 1: Please go to the you "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#" if you are using windows 7 other wise search for "\Snippets\1033" or "\1033" under you VS root directory installation.
Step 2: Once you reach there you will get all code snippet file like this.
Step 3: Please take one code snippet and copy to your desktop and Edit that
CDATA SECTION with the code block you want to see when the code injects.public class SimpleClass{
#region Consts
#endregion
#region Members
#endregion
#region Properties
#endregion
#region Ctor
#endregion
#region Methods
#region API Public Methods
#endregion
#region Private Methods
#endregion
#endregion}
]]>
2. Edit the title part For giving the name.
<br>SimpleClass<br>
3. Change the author name
Fijo Francis T
4. Give a nice description
Class Structure DEFAULT
5. Give a shortcut key
SimpleClass
6. Inside Snippet Section you give the language you are targeting.
]]>
2. Edit the title part For giving the name.
3. Change the author name
4. Give a nice description
Class Structure DEFAULT
5. Give a shortcut key
6. Inside Snippet Section you give the language you are targeting.
or
That's it; you are pretty much finished; the final file looks like this:
xml version="1.0" encoding="utf-8"?><CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header>
<Title> SimpleClass Title>
<Author>Fijo Francis TAuthor> <Description> Class Structure DEFAULT Description>
<Shortcut>SimpleClassShortcut> Header>
<Snippet>
<Code Language="CSharp"> public class SimpleClass
{
#region Consts
#endregion
#region Members
#endregion
#region Properties
#endregion
#region Ctor
#endregion
#region Methods
#region API Public Methods
#endregion
#region Private Methods
#endregion
#endregion
}]]>
Code>
Snippet>
CodeSnippet>CodeSnippets>
Please DO not forget your edited Code snippet in the same name as Title in desktop or any hard drive you copy.
Integration to IDE
Step 1:
PRESS cntl+K && cntl+B in IDE , then it pops up the following window:
Step 2:
Press the import button it and redirect to the code snippet we have saved in drive. And select snippet you created and press open save dialogue window.
Then you get this view:
Please select my code snippet part or you can add under the Visual Studio C# part. What this location is simple. And press Finish you are ready to use code snippet.
Go to IDE type your Title name you and TAB 3 times, there you got your code snippet.
Example
TIP:
Please right on you page and go to insert snippets part it will look like this, so arrange where we need to put our code snippet.
Courtesy - Fijo Francis
No comments:
Post a Comment