Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

Generate Method Stub 

Generate Method Stub is an IntelliSense Automatic Code Generation feature that provides an easy way to have Visual Studio create a new method declaration at the time you are writing a method call. Visual Studio infers the declaration from the call.

Some programming styles, such as test-driven development, suggest that you should consume before you define. That way, it is easier to figure out the form of the API that you are developing. You can use IntelliSense to program in that style. Using the Generate Method Stub operation, you avoid defining everything before you consume it.

The Generate Method Stub IntelliSense operation can also increase productivity because you do not need to move from the calling code, your present focus, to the defining code, a separate focus, in order to generate a new method. You can instead write a method call, and then invoke the Generate Method Stub operation without dividing your attention.

A smart tag indicates that you are able to invoke the Generate Method Stub operation. This smart tag is only available when the cursor is located in the method call, and becomes visible when Visual Studio is not able to locate a method with the same name and number of parameters as a method that is called. When this smart tag is visible, it is displayed under the leftmost character of the method identifier. If you move the cursor off the method call, then this smart tag will disappear.

You can invoke the Generate Method Stub command through a keyboard shortcut, the IntelliSense menu, and from context menu in the Code Editor. For more information, see How to: Generate Method Stub.

The Generate Method Stub command is always available from IntelliSense menu, regardless of the context. This allows overloads that differ only by the type of parameters and not the number of parameters. For example:

   class Program
   {
      static void Method(int a) { }

      static void Main()
      {
         Method("Call"); // No smart tag.
      }
   }

In the preceding example code, a smart tag will not be available when the cursor is position on the call to Method. However, if you invoke the Generate Method Stub command from the IntelliSense menu, then IntelliSense will generate a static method, named Method, which takes a string parameter.

The method stub that is generated can be either instance or static. The generated stub is static if the method is being invoked on the type; otherwise, it is instance.

The names of each parameter, for the method stub that is generated, are derived from the names of the arguments that are passed into the method call. For example:

   class Program
   {
      static void Main()
      {
         string filename = @"file.txt";
         int wordCount = CountWords(filename);
      }
   }

In the preceding example code, Generate Method Stub will generate a method signature that takes a parameter of type string named filename.

The code that is generated by the Generate Method Stub feature is modeled by the code snippet defined in the file MethodStub.snippet. Code Snippets are modifiable. For more information, see How to: Manage Code Snippets.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.