Using Auto Intelligence Using C# Code Snippet for Faster Coding in C#.Net


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


Code1.gif
Code2.gif
Code3.gif

Example With Scenario.

I have created A Silverlight business application like this and added a Class file in the Project like this:

pic3.gif

Step 1: Adding the Copyright Info

When I press 'co', I got my code snippet like this.

pic4.gif

In addition, when I press the tab key 3 times I got all copyright information like this:

pic5.gif

Step 2: Adding the Class Structure

I have revoked auto class template come then it look like this.

pic6.gif

When I type "Simpleclass", then I press the tab key 3 times then I get all Class information like this:

pic7.gif

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:

pic8.gif

pic9.gif

Finally, your page looks like this:

#region CopyRightsAiropse
//<CopyRights>Comments</CopyRights>
//<DevelopedBy>Sonata</DevelopedBy>
//<Developer>Sonataian</Developer>
//<Developed:Date-Started>MM-DD-YY</Developed:Date-Started>
//<Developed:Date-Ended>MM-DD-YY</Developed:Date-Ended>
//<Reason>MM-DD-YY</Reason>
//<Summary> Enter Summary</Summary>
#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

        #endregion

        #region Members

        #endregion

        #region Properties

        #endregion

        #region Ctor

        #endregion

        #region Methods

        #region API Public Methods

        #region SimplePublicMethods
        /// <summary>
        /// My Simple Method Which created By Auto IntelliSense
        /// </summary>
        /// <param name="Value1"></param>
        /// <param name="Value2"></param>
        /// <returns></returns>
        public string SimplePublicMethods(int Value1, int Value2)
        {
            string Value = string.Empty;
            try
            {
 
            }
            catch (Exception ex)
            {

                ErrorWindow.CreateNew(ex.Message);
            }
            return Value;
        }
        #endregion

        #endregion
 
        #region Private Methods

        #region MySimplePrivateMethod
        /// <summary>
        /// My Simple Method Which created By Auto IntelliSense
        /// </summary>
        /// <param name="Value1"></param>
        /// <param name="Value2"></param>
        /// <returns></returns>
        private string MySimplePrivateMethod(int Value1, int Value2)
        {
            string Value = string.Empty;
            try
            {

            }
            catch (Exception ex)
            {

                ErrorWindow.CreateNew(ex.Message);
            }
            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.


pic10.gif

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.

<![CDATA[
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.

<Title>
SimpleClass
</Title>

3. Change the author name

<Author>Fijo Francis T</Author>

4. Give a nice description

<Description>
Class Structure DEFAULT
</Description>

5. Give a shortcut key

<Shortcut>SimpleClass</Shortcut>

6. Inside Snippet Section you give the language you are targeting.

<Code Language="CSharp"> or <Code Language="VB">

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 T</Author>
      <Description>
        Class Structure DEFAULT
      </Description>
      <
Shortcut>SimpleClass</Shortcut>
    </Header>
    <
Snippet>
      <
Code Language="CSharp">
        <![CDATA[
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:

pic11.gif

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.

pic12.gif

Then you get this view:

pic13.gif

pic14.gif

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

pic15.gif

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.

pic16.gif



Points of Interest

Past is always past I am not telling here how we can change already available mess in the current project to new ways but to do things from now on in new ways. I have shamelessly copied ideas from the below MSDN links and sub links to come up with this article. If we think in this direction, we can generalize this more for faster developments.

Some Important Notes

http://msdn.microsoft.com/en-us/library/f7d3wz0k%28v=vs.80%29.aspx
http://msdn.microsoft.com/en-us/magazine/cc302121.aspx


Similar Articles