3

Currently resharper is generating ToString in C# using the string interpolation (i.e. $"Member: {Member}").

Is it possible to modify it so it will use string.Format instead (i.e. string.Format("Member: {0}",Member))

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
duduamar
  • 3,816
  • 7
  • 35
  • 54
  • String interpolation is a feature of C# 6. Are you looking to disable C# 6 support in your project? [That question](http://stackoverflow.com/questions/27621458/disable-c-sharp-6-0-support-in-resharper) has been addressed before. – Gabriel Luci Jan 19 '16 at 13:48
  • 1
    Maybe this could help: ReSharper -> Manage Options -> Code Edition -> C# -> Context Actions -> Disable "Convert to regular string interpolation" Enable "Convert to string.Format" – myst3rium Jan 19 '16 at 13:55
  • 1
    I am just looking to generate ToString using string.Format, I don't want to completely disable C# 6 – duduamar Jan 19 '16 at 14:35

3 Answers3

1

ReSharper > Options > Code Inspection + Inspection Severity > C# > "Language Usage Opportunities" > "Use string interpolation expression". Change this to "Do not show".

Kammersgaard
  • 74
  • 1
  • 2
  • 1
    It doesn't help - it just prevents resharper from showing warnings on string.Format, but it still generates ToString using string interpolation. – duduamar Jan 19 '16 at 14:20
0

I just stumbled over the same problem in Resharper 2016.1

For me it helped to position the caret over the + operator in a string concatenation and hit Alt+Enter. It then lists an option to refactor to string.Format. Positioning the caret over the string does not work. The option will not be available in that case.

This must be a recent change because I use that refactoring often for localization reasons.

BlueM
  • 6,523
  • 1
  • 25
  • 30
-1

It might be worth checking out Fody.ToString project, it can automatically create an overload of your ToString method after build using IL Weaving:

Your Code:

[ToString]
class TestClass
{
    public int Foo { get; set; }

    public double Bar { get; set; }

    [IgnoreDuringToString]
    public string Baz { get; set; }
}

Gets converted to:

class TestClass
{
    public int Foo { get; set; }

    public double Bar { get; set; }

    public string Baz { get; set; }

    public override string ToString()
    {
        return string.Format(
            CultureInfo.InvariantCulture, 
            "{{T: TestClass, Foo: {0}, Bar: {1}}}",
            this.Foo,
            this.Bar);
    }
}

Check it out, right time saver - https://github.com/Fody/ToString

Kevin Smith
  • 13,746
  • 4
  • 52
  • 77