3

I'm testing migrating a .NET 6.0 application to .NET 8.0, and the following piece of code now throws CA1863 "Use 'CompositeFormat'". But I don't know how.

private const string DUMP_DIALOGS_TITLE_FORMAT_STRING = "{0} Diagnostic Report";

public static string GetTitle(IProcess currentProcess)
{
    return string.Format(CultureInfo.InvariantCulture, DUMP_DIALOGS_TITLE_FORMAT_STRING, currentProcess.ProcessName);
}

The example given here is invalid, since the StaticField variable isn't initialized and I don't know what it should be.

If I change the code to

private static readonly CompositeFormat Format = CompositeFormat.Parse("{0} Diagnostic Report");

public static string GetTitle(IProcess currentProcess)
{
    return string.Format(CultureInfo.InvariantCulture, Format, currentProcess.ProcessName);
}

Visual Studio shows a warning that I'm not providing a parameter for the {0} argument, even though everything appears to work as expected (I'm not getting a runtime error with the new code). Is it just that VS code analysis isn't knowing about CompositeFormat yet or am I missing something?

3
  • 1
    I don't observe this issue in VS 17.8 Commented Nov 28, 2023 at 12:15
  • Doubling Edgars's observation, here dotnet.microsoft.com/en-us/download/dotnet/8.0 it states that .NET 8 is supported by Visual Studio 2022 (v17.8) Commented Nov 28, 2023 at 12:24
  • I have v17.9 Preview 1, so I'm assuming it should be supported, too. I have also Resharper installed, which might be the culpit here. Commented Nov 28, 2023 at 12:25

1 Answer 1

4

Your implementation is just fine:

private static readonly CompositeFormat Format = CompositeFormat.Parse("{0} Diagnostic Report");

public static string GetTitle(IProcess currentProcess)
{
    return string.Format(CultureInfo.InvariantCulture, Format, currentProcess.ProcessName);
}

The warning comes from ReSharper which currently has only basic support of .NET 8 features (see the .NET SDK support in JetBrains Rider doc.).

Either install the EAP/RC version (link) which should have the issue fixed or disable the warning with comment for now:

// ReSharper disable once FormatStringProblem
private static readonly CompositeFormat Format = CompositeFormat.Parse("{0} Diagnostic Report");

P.S.

Created a PR for the docs to make example potentially more clear.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.