Friday, July 27, 2007

Beauty of Code Snippets

Although I am using Visual Studio.NET 2005 from more than an year & although I am aware of code snippets in VS.NET, never used it until recently one of my friends told me to find its beauty.

just type the keyword 'while' in your visual studio.net 2005 editor, you can see this something like this:




after typing the word while press tab twice & You will find its complete syntax on your page.


This is the code that I have got in visual studio after typing the word exception and tab twice. AWESOME!! Right!


--------------------------------------------------------------------------
[global::System.Serializable]
public class MyException : Exception
{
//
// For guidelines regarding the creation of new exception types, see
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
// and
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
//

public MyException() { }
public MyException(string message) : base(message) { }
public MyException(string message, Exception inner) : base(message, inner) { }
protected MyException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}


-------------------------------------------------------------------------
code for creating custom attributes (type in attribute and tab twice)
-------------------------------------------------------------------------
[global::System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class MyAttribute : Attribute
{
// See the attribute guidelines at
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconusingattributeclasses.asp
readonly string _positionalString;
int _namedInt;

// This is a positional argument.
public MyAttribute(string positionalString)
{
this._positionalString = positionalString;

// TODO: Implement code here.
throw new Exception("The method or operation is not implemented.");
}
public string PositionalString
{
get
{
return this._positionalString;
}
}
// This is a named argument.
public int NamedInt
{
get
{
return this._namedInt;
}
set
{
this._namedInt = value;
}
}
}
--------------------------------------------------------------------------

Similarly we can explore iterators, for, while, interfaces etc...


Thank you my best friend for reminding me of this feature!

1 comment:

Anonymous said...

Keep up the good work.