Create methods from code snippets in Visual Studio .NET
By Shivprasad Koirala
In this section we will see a very interesting and useful trick of visual studio called the extract method.
Visual studio tip 2: Create methods from code snippets:-
In this section we will see a very interesting and useful trick of visual studio
called as the extract method. Below are two lines of code which help us to display
the current year of the system. We expect that we would need to display the
current year from different parts of program.
static void Main(string[] args)
{
int CurrentYear = DateTime.Now.Year;
Console.WriteLine(CurrentYear.ToString());
}
So as a good practice we would like to wrap these 2 lines of code in to a method
so that we can call it again and again in other sections of the code and thus
increase reusability and reduce redudancy.
One way of doing the same is by creating a method manually and then moving those
two lines code of in to the method.
For such kind of scenario visual studio has provided a nice facility for automation
called as extract method. Select the lines of code which you want to move to
a method , right click and then click on extract method as shown in the below
image.

Once you click on extract method you will be asked to input the method name as shown
in the below image.

Once you put the method name and click ok , you should see those two lines of code
are moved in to a separate method and the method name is invoked in static void
main as shown in the below code snippet. All automated LOL.
static void Main(string[] args)
{
DisplayCurrentYear();
}
private static void DisplayCurrentYear()
{
int CurrentYear = DateTime.Now.Year;
Console.WriteLine(CurrentYear.ToString());
}
Related FAQs
Many times as a developer you come across functions with lots of input parameters as shown in the below code snippets. In real projects the input parameters would be much higher as compared to the below code snippets. Some times for various reasons you want to shuffle them, reorder them or remove some of them.
Many times due to project pressure and lazy attitude you violate encapsulation and create public variables for classes as shown below. Even though your inner heart knows that the best practice is to create set and get property function but your lazy attitude overrules it.
We all know exceptions get propagated from the last caller to the main caller. For instance let’s say from your static void main method you are calling “SomeMethod” and he in turn is calling “SomeMethod1”.
While debugging you often want to skip debugging on certain lines of code. For instance in the below code you have set the debug point to the first line, you would like to skip the in between lines and jump directly to “console.writeline” step.
In big project you have 100’s of classes and each of those classes can have lots of properties.
As developer debugging is your routine job and you would like your debugger to debug smartly rather than monotonously. For instance in the below code we have put a debug point and we do not want our debugger to just break monotonously.
Create methods from code snippets in Visual Studio .NET (3677 Views)