Rajesh kumar’s Blog

If you have knowledge, let others light their candles at it.

C# Code Snippets

Posted by Rajesh Kumar on May 14, 2009

 Hello All,

Visual Studio IDE provides some code snippets that reduces code typing. You can open the “Code Snippet Manager” from Visual Studio from the menu: Tools -> Code Snippet Manager.

I am giving the C# code snippets below for your quick understanding. 

Shortcut

Description

Snippet Code

#if

 

#if true

#endif

#region

 

#region MyRegion   

#endregion

~

Snippet for Class Destructor

~Class1()

{

}

Attribute

 

[global::System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]

sealed class MyAttribute : Attribute

{

    // See the attribute guidelines at

    // http://go.microsoft.com/fwlink/?LinkId=85236

    readonly string positionalString;

     // This is a positional argument

    public MyAttribute(string positionalString)

    {

         this.positionalString = positionalString;

          // TODO: Implement code here

         throw new NotImplementedException();

     }

      public string PositionalString

     {

         get { return positionalString; }

     }

      // This is a named argument

     public int NamedInt { get; set; }

}

checked

 

checked

{

}

class

 

class MyClass

{

}

ctor

 

public Class1()

{

}

cw

 

Console.WriteLine();

do

 

do

{

} while (true);

else

 

else

{

}

enum

 

enum MyEnum

{

}

equals

Overrides the “Equals” method

// override object.Equals

public override bool Equals(object obj)

{

    //       See the full list of guidelines at

    //  http://go.microsoft.com/fwlink/?LinkID=85237 

    // and also the guidance for operator== at

    // http://go.microsoft.com/fwlink/?LinkId=85238

    //

     if (obj == null || GetType() != obj.GetType())

    {

        return false;

    }

     // TODO: write your implementation of Equals() here

    throw new NotImplementedException();

    return base.Equals(obj);

}

 // override object.GetHashCode

public override int GetHashCode()

{

   // TODO: write your implementation of                       

           GetHashCode() here

   throw new NotImplementedException();

   return base.GetHashCode();

}

Exception

 

[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

 

    public MyException() { }

    public MyException(string message) : base(message)

    { }

    public MyException(string message, Exception inner) : base (message, inner) { }

    protected MyException(              global::System.Runtime.Serialization.SerializationInfo
info,   global::System.Runtime.Serialization.StreamingContext
context): base(info, context) { }

}

for

 

for (int i = 0; i < length; i++)

{

}

foreach

 

foreach (var item in collection)

{

}

forr

 

for (int i = length – 1; i >= 0 ; i–)

{

}

if

 

if (true)

{

}

indexer

 

public object this[int index]

{

   get { /* return the specified index here */ }

   set { /* set the specified index to value here */ }

}

interface

 

interface IInterface

{

}

invoke

Safely invoke the event

EventHandler temp = MyEvent;

if (temp != null)

{

    temp();

}

iterator

 

public System.Collections.Generic.IEnumerator<ElementType>
GetEnumerator()

{

    throw new NotImplementedException();

    yield return default(ElementType);

}

Iterindex

Code snippet for named iterator/indexer
pair using nested class

public MyViewIterator MyView

{

    get

    {

         return new MyViewIterator(this);

    }

}

 

public class MyViewIterator

{

     readonly Class1 outer;

      internal MyViewIterator(Class1 outer)

     {

          this.outer = outer;

     }

 

     // TODO: provide an appropriate implementation here

     public int Length { get { return 1; } }

      public ElementType this[int index]

     {

         get

         {

            //

            // TODO: implement indexer here

            // you have full access to Class1 privates

            //

            throw new NotImplementedException();

            return default(ElementType);

         }

      }

 

      public System.Collections.Generic.IEnumerator<ElementType>
GetEnumerator()

      {

          for (int i = 0; i < this.Length; i++)

          {

              yield return this[i];

          }

      }

}

lock

 

lock (this)

{

}

mbox

 

global::System.Windows.Forms.MessageBox.Show(“Test”);

namespace

 

namespace MyNamespace

{

}

prop

 

public int MyProperty { get; set; }

propg

 

public int MyProperty { get; private set; }

sim

 

static int Main(string[] args)

{

   return 0;

}

struct

 

struct MyStruct

{

}

svm

 

static void Main(string[] args)

{

}

switch

 

switch (switch_on)

{

   default:

}

try

 

try

{            

}

catch (Exception)

{

    throw;

}

tryf

 

try

{            

}

finally

{

}

unchecked

 

unchecked

{

}

unsafe

 

unsafe

{

}

using

 

using(resource)

{

}

while

 

while (true)

{

}

 

Advertisements

One Response to “C# Code Snippets”

  1. Very interesting blog! Subscribed on rss. Regular will read it

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

 
%d bloggers like this: