Search code examples
c#namespacescommentscurly-bracesglobal-namespace

Global Namespace Alias (double colon) with curly brackets in comment - C#


I want to call some method in curly brackets in a comment, for example like this:

$"{global::Company.Company.Helpers.Method()}_{Parameters.ProcessName}";

But in this situation everything behind the colon become a comment. Is there a way to use double colon in curly brackets in comment in C#?


Solution

  • This is likely due to the way that string formatting works - {thing:A} will format thing with "A" as format information. For example, formatting an enum as {myEnumValue:D} will use the integer value, but {myEnumValue:G} will use the name of the enum value.

    You can work around this by wrapping the colon-containing expression in parentheses, like so:

    $"{(global::Company.Company.Helpers.Method())}_{Parameters.ProcessName}"