1

I'm attempting to insert a person's name into the body of an email as dynamic values using string interpolation in c# 6.0.

Proof of Concept

public class Program
{
    public static void Main()
    {
        var p = new Person { FirstName = "John", LastName = "Smith" };
        var msg = $"{p.FirstName} says 'Hello World!'";
        
        Console.WriteLine($"Message: {msg}");
    }
}
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

This proof of concept outputs the string:

Message: John says 'Hello World!'

Implementation

So now when I'm trying to use the concept to build my MailMessage, I'm getting {Contact.FirstName} output in my email. Here's the code:

private string _Body;
public string Body
{
    get { return _Body; }
    set
    {
        _Body = value;
        RaisePropertyChangedEvent("Body");
    }
}

var Contacts = Context.Contacts
  .Where(x => x.ContactGroupId == ContactGroupId && x.Deleted == false);

// By this point, Body will already have a value containing the interpolation.
// For example: Body = "Message: {Contact.Name} says \"Hello World!\"";
foreach (var Contact in Contacts)
{
    MailMessage Msg = new MailMessage();
    
    MailMessage.Body = ${Body}
}

Problem

This should render the body to read as:

Message: John says "Hello World!"

Instead, what renders is:

Message: {Contact.FirstName} says "Hello World!"

How can I interpolate my variables into the string if I'm not actually hard coding the string in my code?

4
  • 8
    String interpolation is a compile time feature. It cannot be used to replace tokens at runtime. Commented Jul 21, 2017 at 11:07
  • 1
    Your code shouldn't even compile. Commented Jul 21, 2017 at 11:10
  • 2
    Should be MailMessage.Body = $"{Body}"... Commented Jul 21, 2017 at 11:11
  • weblogs.asp.net/bleroy/… Commented Feb 14, 2020 at 13:49

2 Answers 2

4

${Body} doesn't compile.

You can't generate the interpolation at run-time, it is a syntactic sugar that gets transformed by the compiler into String.Format.

You can solve your problem by using Replace from StringBuilder or String:

sb.Replace("{p.FirstName}", p.FirstName);
Sign up to request clarification or add additional context in comments.

6 Comments

String.Format is fine but won't it have the same problem? Anyway I can't have the user typing in {0} {1} etc tags in the mail body when he's writing the email... Suppose I could always leave it as {Contact.FirstName} and then .Replace() the hell out of it...
@Ortund Yes, that's what you need to do, though I suggest using StringBuilder for that.
@Ortund You can solve your problem multiple ways, String.Replace is one of the options. String.Replace("{p.FirstName}", p.FirstName); StringBuilder is a better option though.
I guess either way I go, the issue of interpolation is now moot so accepting an answer becomes a strange issue
@Ortund Actually it's a valid question, I'm sure other people have had the same issue.
|
0

String interpolation is a compile-time feature. That’s why you can do some neat things like:

Console.WriteLine($"When {condition} is true, {(condition ? "it's true!" : "It's False")}");

You are essentially putting C# code inside the string!

However, if it were implemented as a dynamic feature where you could load strings and have them evaluate at runtime, then you would have a security risk much like the use of eval in JavaScript. It makes sense, then, that it wouldn’t be possible to do what you want to do.

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.