Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper reorder String.Format() arguments

Tags:

c#

resharper

I often find myself writing something daft like:

String.Format("{1}: {0}", reason, message);

or something similar but with far more string placeholders.

Is there an automated refactoring in ReSharper to change the order of the placeholders and the arguments? I have a tendency to mess up the mapping if I try to change the order by hand.

Obviously, the example above is trivial. What I am doing in reality is often writing something like:

String.Format("{0}.{2} = {1}.{3} AND {0}.{4} = {1}.{5} AND {6}.{7} = {1}.{8}",
  table1Alias, table2Alias, col1A, col2A, col1B, col2B, table3Alias, col3C, col2C);

and thinking to myself that it would be great if I could move table3Alias up to the front next to the other aliases.

(ReSharper 7.1.3)

like image 828
Jonny Avatar asked Sep 19 '13 13:09

Jonny


1 Answers

For C# 6+ (ReSharper 10+ / 2016+):

  • Place your cursor at string.Format
  • Press Alt + Enter
  • Select Use string interpolation
  • Press Alt + Enter again
  • Select Convert to string.Format
like image 198
Chris Xue Avatar answered Sep 24 '22 10:09

Chris Xue