When starting your adventure with programming, it is worth sticking to good practices from the very beginning. It's not just about writing code, but also about using the tools we use. In today's article, I wanted to share with you the most popular visual studio snippets that I use every day at work. By using them, you will definitely increase your productivity and they will definitely make your work easier.
What are snippets?
These are code functions available in the IDE that make writing code faster. If you want to generate a given piece of code, just enter the keyword and then the tab key. All snippets that I will present in this article are available by default without any additional configuration in Visual Studio.
#1 Creating a class - class
class MyClass
{
}
#2 Creating an interface - interface
interface IInterface
{
}
#3 Creating an enum - enum
enum MyEnum
{
}
#4 Creating a constructor - ctor
public Program()
{
}
#5 Creating properties - prop
public int MyProperty { get; set; }
#6 Creating properties with a field - propfull
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
#7 Creating a for loop with increment - for
for (int i = 0; i < length; i++)
{
}
#8 Creating a for loop with decrementation - forr
for (int i = length - 1; i >= 0; i--)
{
}
#9 Creating a loop do while - do
do
{
} while (true);
#10 Creating a loop while - while
while (true)
{
}
#11 Creating a loop foreach - foreach
foreach (var item in collection)
{
}
#12 Creating an if statement - if
if (true)
{
}
#13 Creating a switch statement - switch
switch (switch_on)
{
default:
}
#14 Displaying messages in the desktop application - mbox
MessageBox.Show("Test");
#15 Displaying messages in the console application - cw
Console.WriteLine();
#16 Generating get actions in mvc - mvcaction4
public ActionResult Action()
{
return View();
}
#17 Generating post actions in mvc - mvcpostaction4
[HttpPost]
public ActionResult Action()
{
return View();
}
#18 Generating a try catch block - try
try
{
}
catch (Exception)
{
throw;
}
#19 Generating a try finally block - tryf
try
{
}
finally
{
}
#20 Generating a using block - using
using (resource)
{
}
#21 Generating methods to override the object comparison method - equals
// 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();
}
SUMMARY:
That's it for the most popular snippets available by default in Visual Studio. If you don't know these snippets yet, be sure to start using them today. Of course, if you want, you can generate your own additional snippets in Visual Studio. I will show you how to do it step by step and what additional snippets I use in the next article. And which of the default snippets available in Visual Studio do you use most often? :)