Sunday, September 18, 2011

Code Shortcuts in Visual Studio 2010

1. To define a Destructor

~ + tab tab




Code snippet:

~_Default()
{
}


2. To create an Attribute

attribute + tab tab




Code snippet:

[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; }
}



3. Checked keyword

checked + tab tab




Code snippet:

checked
{
}



4. Creating a Class

class + tab tab




Code snippet:

class MyClass
{
}


5. Creating a Constructor

ctor + tab tab




Code snippet:

public _Default()
{ }



6. Console.WriteLine() shortcut

cw + tab tab




Code snippet:

Console.WriteLine ();


7. Do…..while loop

do + tab tab




Code snippet:

do
{
} while (true);


8. Else statement

else + tab tab




Code snippet:

else
{}


9. Enum

enum + tab tab




Code snippet:

enum MyEnum
{
}



10. Equals

equals +tab tab




Code snippet:

// 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();
}


11. Exceptions

exceptions + tab tab




Code snippet:

[Serializable]
public class MyException : Exception
{
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) { }
}


12. For loop

for + tab tab




Code snippet:

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


13. Foreach loop

foreach + tab tab




Code snippet:

foreach (var item in collection)
{
}


14. Reverse for loop

for + tab tab




Code snippet:

for (int i = length - 1; i >= 0 ; i--)
{
}



15. If statement

if + tab tab




Code snippet:

if (true)
{
}


16. Indexer

indexer + tab tab




Code snippet:

public object this[int index]
{
get { /* return the specified index here */ }
set { /* set the specified index to value here */ }
}



17. Interface

interface + tab + tab




Code snippet:

interface IInterface
{
}


18. Invoke- to safely invoke the event

invoke + tab tab




Code snippet:

EventHandler temp = MyEvent;
if (temp != null)
{
temp();
}


19. Iterator- simple iterator

iterator + tab tab




Code snippet:

public System.Collections.Generic.IEnumerator GetEnumerator()
{
throw new NotImplementedException();
yield return default(ElementType);
}


20. Iterator – complex iterator

iterindex + tab tab




Code snippet:

public MyViewIterator MyView
{
get
{
return new MyViewIterator(this);
}
}
public class MyViewIterator
{
readonly _Default outer;
internal MyViewIterator(_Default 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 _Default privates
//
throw new NotImplementedException();
return default(ElementType);
}
}
public System.Collections.Generic.IEnumerator GetEnumerator()
{
for (int i = 0; i < this.Length; i++)
{
yield return this[i];
}
}
}


21. Lock statement

lock + tab tab




Code snippet:

lock (this)
{}


22. To Show the Message box in ASP.Net/Console Application

mbox + tab tab




Code snippet:

global::System.Windows.Forms.MessageBox.Show("hello");


23. Action

mvcaction +tab tab




Code snippet:

public ActionResult Action()
{
return View();
}


24. Action via http post method

mvcpostaction + tab tab




Code snippet:

[HttpPost]
public ActionResult Action()
{
return View();
}


25. For Namespace

namespace + tab tab




Code snippet:

namespace MyNamespace
{
}


26. Creating Property

prop + tab tab




Code snippet:

public int MyProperty { get; set; }


27. Attached dependency property

propa + tab tab




Code snippet:

public static int GetMyProperty(DependencyObject obj)
{
return (int)obj.GetValue(MyPropertyProperty);
}

public static void SetMyProperty(DependencyObject obj, int value)
{
obj.SetValue(MyPropertyProperty, value);
}

// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("MyProperty", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0));



28. Dependency property as a backing store

propdp + tab tab




Code snippet:

public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0));



29. Property with full description

propfull + tab tab




Code snippet:

private int myVar;

public int MyProperty
{
get { return myVar;}
set { myVar = value;}
}


30. Auto implemented property with get accessor as private

propg + tab tab




Code snippet:

public int MyProperty { get; set; }



31. For int Main method

sim + tab tab




Code snippet:

static int Main(string[] args)
{
return 0;
}


32. Create structure

struct + tab tab




Code snippet:

struct MyStruct
{
}


33. Void Main method

svm + tab tab




Code snippet:

static void Main(string[] args)
{}


34. Switch stamen

switch + tab tab




Code snippet:

switch (switch_on)
{
default:
}


35. Test class

testc + tab tab




Code snippet:

[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestClass]
public class MyTestClass
{
}


36. Test method

testm + tab tab




Code snippet:

[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
public void MyTestMethod()
{
}


37. Try..catch

try + tab tab




Code snippet:

try
{}
catch (Exception)
{
throw;
}


38. Try..finally

tryf + tab tab




Code snippet:

try
{}
catch (Exception)
{
throw;
}


39. Unchecked statement

unchecked + tab tab




Code snippet:

unchecked
{
}


40. Unsafe statement

unsafe + tab tab




Code snippet:

unsafe
{
}


41. Using statement

using + tab tab




Code snippet:

using (resource)
{
}


42. Dependency property event handler in windows work flow application

wde + tab tab




Code snippet:

public static global::System.Workflow.ComponentModel.DependencyProperty InvokeEvent = global::System.Workflow.ComponentModel.DependencyProperty.Register("Invoke", typeof(EventHandler), typeof(_Default));

[System.ComponentModel.Description("Invoke")]
[System.ComponentModel.Category("Invoke Category")]
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible)]
public event EventHandler Invoke
{
add
{
base.AddHandler(_Default.InvokeEvent, value);
}
remove
{
base.RemoveHandler(_Default.InvokeEvent, value);
}
}


43. Dependency property in windows work-flow application

wdp + tab tab




Code snippet:

public static global::System.Workflow.ComponentModel.DependencyProperty MyPropertyProperty = global::System.Workflow.ComponentModel.DependencyProperty.Register("MyProperty", typeof(string), typeof(_Default));

[System.ComponentModel.Description("MyProperty")]
[System.ComponentModel.Category("MyProperty Category")]
[System.ComponentModel.Browsable(true)]
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible)]
public string MyProperty
{
get
{
return ((string)(base.GetValue(_Default.MyPropertyProperty)));
}
set
{
base.SetValue(_Default.MyPropertyProperty, value);
}
}



44. While loop

while + tab tab




Code snippet:

while (true)
{
}

No comments:

Post a Comment