CodeRush – Feature Spotlight – Convert to Function

Assuming you want to create a new function, there are a few ways you could build it.

For now let’s focus on the function’s return type…

Type First

In this case, you know the type of the value which your function will return.

You have decided that you want to return a integer.

The simplest way to do this is using the mi<space> template, which will result in the code below.


All that’s needed is:

  • An appropriate name.
  • Any required parameters.
  • The body of the method.

…and you’re done.

But what if, in that first moment, you don’t know what type you’d like to return from your method?

Type Last

If you’re not entirely sure of the type your method should be returning, don’t worry…. Just don’t specify one …. yet.

Use CodeRush’s m<space> template to generate the method which has no return value. Otherwise known as a void proc.


As before you have to specify:

  • An appropriate name
  • Any parameters
  • The body of the method.

However let’s suppose that you’ve done this and you have your brand new Return42 function.


You’ll notice that at this point our function doesn’t compile. This is, of course, because we are attempting to return an int from a method which specifies a return type of void.

This is clearly unacceptable and needs fixing. Ordinarily we’d visit the signature of the function, highlight the word void and overtype it with int.

This can be rather annoying. However CodeRush has our back.

Simply place your caret on the return keyword and select Convert to Function from the smart tag menu.


CodeRush deduces the type of the expression being returned, and alters the signature of the method to int rather than void.

In our example, the involved type was easy to deduce and relatively simple, but the procedure works with more complex expressions as well.



People aren’t all the same, and work in a myriad different ways.

I’m one of those people who tend not to think in terms of the specific type I’m returning, until the point of actually returning a value.

CodeRush doesn’t force me to think about this, and is happy to wait until I’m ready.

When I am ready, CodeRush still has my back with tools to help achieve my goals in my way.

New CodeRush Plugin – CR_RemoveStringToken

A new plugin… What does it do?

Example 1

So imagine we have the following code..


Imagine now, that we decide that it’s not important to explain ‘where’ we were searching, when reporting this error.

Normally we would…

  • Highlight the string token {1} (including it’s associated text).
  • Hit delete.
  • Highlight the “Y” argument (including it’s leading comma).
  • Hit delete.

RemoveStringToken allows us to remove the token and it’s associated parameter in a single step.

  • We place our caret on the token we want to remove ({1} in this case)
  • Choose Remove String Token from the Code menu.


A little tidying is required to remove ‘ in ‘, but that’s a whole lot simpler than before.

Example 2

In this next example, things are slightly more complicated.


Users aren’t terribly good with codes, so we’ve decided to remove token {1} from the message.

The manual steps to achieve this are mostly the same as the previous example.

  • Highlight the string token {1} (including it’s associated text)
  • Hit delete.
  • Highlight the “Y” argument. (including it’s leading comma)
  • Hit delete.

However this time there is an additional requirement.

Since we’re removing a string token from the middle of the sequence, we need to renumber the tokens that are numerically higher than the one we’re removing.

So this time our manual steps would also include:

  • Renumber references to {2} so they reference {1} instead.

Of course if we had additional references to {1} in your string, we’d have to remember not to renumber those other tokens.

I’m sure you agree that’s a bit of a pain.

RemoveStringToken handles the token removal, the parameter removal and the token renumbering, all in a single step.


Once again there are some additional characters in need of a trim. but these are easily dealt with.

Checks and Balances

It’s worth noting that with any simple sequence of steps that a human might make, there is always the possibility of mistakes.

RemoveStringToken checks several things automatically every time, so you don’t have to.

  • Arguments are only removed if there are no remaining references to the target token.
  • Tokens are only renumbered if there are no remaining references to the target token.
  • Console.WriteLine and String.Format are both supported.
  • If no tokens remain within the string, and the outer call is to String.Format, then this outer call is removed.


As usual this plugin is available from the Visual Studio Gallery and from GitHub.

The source code is available on GitHub


New CodeRush Plugin–CR_ReverseOperands

This plugin is a natural follow up to my previous CR_ReverseArgs plugin

As you might imagine, this plugin changes the order of the selected operands.


This might seem like a fairly trivial reason to create a plugin, but it happens often that it irritates me, and it does solve the problem. Hopefully it will also save you some time.


As usual this plugin is available from The Visual Studio Gallery and from GitHub.

The source code is also available on GitHub.

New CodeRush Plugin - CR_ReverseArgs

Hurrah, another new CodeRush plugin.

The problem minor annoyance

Occasionally I realise that I’ve passed a couple of arguments to a method in the wrong order.

What follows is usually a complicated series of…

  • Move Left (Ctrl+Left)
  • Select Right (Shift+Ctrl+Right)
  • Cut Argument (Ctrl+X)
  • Move Right (Ctrl+Right)
  • Paste Argument (Ctrl+V)

The navigation operations usually need to be repeated several times in order to get into the correct position.

Additionally there’s that bit where you either…

  • Try to judge which comma to include in the cut/paste operation.
  • Or… Delete the comma, and enter it back again once the paste is complete.

Add this to the fact that I usually have something useful on the clipboard, which I’d prefer not to have to burn whilst correcting the order of these arguments.

The whole rigmarole is something to be avoided.

Enter CR_ReverseArgs

The new CR_ReverseArgs plugin allows you to just highlight the pair of arguments, and trigger Reverse Args from the CodeRush Smart Tag menu.

Then CodeRush will do the rest.


This CodeProvider should work with ints, strings, and all sorts of other objects. The 2 arguments need not be of the same type either. Feel free to switch whichever pairs you like.

Note: This plugin is NOT a Refactoring. It will affect the functionality of your code.
If you wish to move both the Arguments and their corresponding Parameters, then you should use the Reorder Parameters refactoring


This plugin is available immediately:


New CodeRush Plugin - Declare Extension Method

Another new CodeRush plugin!

This time one to help declare extension methods on existing objects.

Introducing Declare Extension Method

This plugin is really simple.

Consider the following code with a call to an undeclared method MyNewMethod.


Place your caret on the call to MyNewMethod and hit your CodeRush Refactor key (Defaults to Ctrl+`)


Choose ‘Declare Extension Method’ from the menu.


CodeRush generates a new static class in the same namespace as your calling code, containing the stub of your new extension method.

The previous syntax error will have disappeared and all that’s left to do, is for you to fill in the implementation of your new method Smile

Where do I get this Awesome Plugin from ?

Declare Extension Method is available from

GitHub: Here you will find the current (and historical) source of the plugin as well as the latest installable version.

The Visual Studio Gallery: Installing from the Visual Studio Gallery allows VS to locate any updates that are shipped after the initial release.

More Posts