Visual studio tip 2: Create methods from code snippets

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

When we select source code in visual studio it selects the complete source code , but what if we want to select the code in a rectangular manner?
Now many times as a .NET developer we need connection strings to connect to databases. Connection string are long and cryptic and very difficult to remember. In this tip we will see a easy way of getting connection strings.
Many times we write comments in code to remind about certain tasks to be performed later and then we forget about the same as the comments gets lost in those million lines code.
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.
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.
As a developer you always need some common codes again and again. Rather than writing those common codes from scratch you would like to get them easily from some handy place.
Visual studio tip 2: Create methods from code snippets  (755 Views)
Create New Account