Search code examples
c#string-interpolation

Interpolating a string in c#


I want to use string interpolation for the below:

payloadArgs = string.Format("{0}TrackingID: \"{1}\"", payload.ToString().Replace("\n", "; "), trackingId);

I have tried

payloadArgs = $"{trackingId} TrackingId, \"{payload.ToString().Replace("\n", "; "), trackingId};

but I am obviously getting it wrong as I am getting a compile error "Cannot implicitly convert a string to int" over the "trackingId" and a warning about "newline in constant"

What's the correct way to do this?


Solution

  • You are close. You changed the order between the variables, you had trackingId as the first variable instead as the second. In addition, string interpolation doesn't receive parameters in the structure of (string, parameter, parameter), it get it inside the curly brackets

    payloadArgs = $"{payload.ToString().Replace("\n", "; ")}TrackingID: \"{trackingId}\"";