Share via


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 that 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 determine the form of the API that you are developing. You can use IntelliSense to program in that style. The Generate Method Stub operation helps you avoid defining everything before you consume it.

The Generate Method Stub IntelliSense operation can also increase productivity because you do not have 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.

Remarks

Invoking through Smart Tags

A smart tag indicates that you can 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, this smart tag disappears.

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

Invoking Manually

The Generate Method Stub command is always available from the IntelliSense menu in all contexts. This allows for 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 previous example code, a smart tag is available when the cursor is position on the call to Method. However, if you invoke the Generate Method Stub command from the IntelliSense menu, IntelliSense generates a static method that is 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.

Parameter Names

The names of each parameter for the generated method stub 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 previous 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.

Ambiguous Types

Generate Method Stub uses Object when the type of the parameter or return value cannot be inferred, such as in anonymous types or implicitly typed local variables (keyword var). For example:

   class Program
   {
      static void Main()
      {
         var filename = Method(); //Cannot infer the return type
      }
   }

In the previous example, invoking Generate method Stub on Method()causes a method stub that returns Object.

See Also

Tasks

How to: Generate Method Stub

Other Resources

Automatic Code Generation