6

We would like to maintain the emails that are sent from our ASP.NET Web Application in a database. The idea was that the format of emails are stored in a database.

The problem is that emails should include order specific information, e.g.:

Thank you for your order John Smith,

your order 1234 has been received

What I'm trying to achieve is that I have used string verbatims in the database column values where it would be stored like this:

Thank you for your order {o.customer},

your order {o.id} has been received

I'm curious as to whether it is possible to do string interpolation where the values are already in the string that is formatted. If I try to use String.Format(dbEmailString) it throws me exception:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: Input string was not in a correct format.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
johnnyCasual
  • 103
  • 2
  • 8

3 Answers3

10

String interpolation is a compile time feature. You can do what you want using the regular String.Format:

var formattedEmail = String.Format("Thank you for your order {0}, your order {1} has been received", o.customer, o.id);

If you've got a requirement to replace out various different placeholders with the values, perhaps you need to be looking for a more dynamic templating system, a very basic one could be done with String.Replace.

The bottom line is you can't reference variables in your code directly from stored strings in this fashion - you'll need to process the string in some way at runtime.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • I think this answered to my quickly formulated question. What I would need would be too dynamic in nature where number of placeholders might be different. I think I should try to get around it different way, probably with String.Replace – johnnyCasual Jul 13 '16 at 15:34
  • @johnnyCasual If you go that route, you could of course keep the exact same placeholders you already have, and simply replace them out, eg: `email = email.Replace("{o.customer}", o.customer)` - that way you can keep the names the same as the variables if you wish. – James Thorpe Jul 13 '16 at 15:38
  • Don't forget about Regex.Replace and T4 Text as well. both if these can do more powerful work in formatting a string. – iQueue Aug 24 '18 at 15:31
-1

In c# 6, the interpolated string expression looks like a template string that contains expressions. It looks like the String.Format() placeholders, but instead of an index, it is the expression itself inside the curly braces.
it looks like String.Format() in C#6 is a syntactical sugar that the compiler treats like String.Format() behind the scenes.

example, to display the expressions in OP in vs 2015:

class Program
{
    static void Main(string[] args)
    {

        dynamic order = new {customer = "Robert", id=123};
        PrintString(order);            
    }


    static void PrintString(dynamic o)
    {
        var exp1 = $"Thank you for your order { o.customer}";
        var exp2 = $"your order { o.id} has been received";
        Console.WriteLine(exp1);
        Console.WriteLine(exp2);
         
    }

}

The output is:

Thank you for your order Robert

your order 123 has been received

You can retrieve the formatted expressions from the database and prefix them with "$", as described in the example above.

Under VS 2013 you can install the new compilers into the project as a nuget package: Install-Package Microsoft.Net.Compilers

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • 4
    _"You can retrieve the formatted expressions from the database and prefix them with "$", as described in the example above."_ No, you can't. Static compile time strings [are fine](http://i.stack.imgur.com/0Wnq6.png) with interpolation, dynamic strings from databases [won't work](http://i.stack.imgur.com/c6ykG.png) – James Thorpe Jul 14 '16 at 07:26
-2

I think you mean the new feature of C# 6.0: https://msdn.microsoft.com/en-us/library/dn961160.aspx

I only read about it and have no experience yet. But it must work with customer and id variables and I have no sign wether it works or not with an object's properties.

string customer = o.customer; int id = o.id; string msg = $"Thank you for your order {customer},your order {id} has been received";

gives you the correct result. (VS 2015 and .Net 4.6 provided.)

string msg = $"Thank you for your order {o.customer}, your order {o.id} has been received";`

It may work, I have no clues.

Peter Krassoi
  • 571
  • 3
  • 11
  • Yeah I tried to achieve that but the problem was that I couldn't use string msg = $dbEmailString. It didn't allow $ prefix for string variable – johnnyCasual Jul 13 '16 at 15:55
  • I missed this sentence: "The idea was that the format of emails are stored in a database." The interpolated string is a compile time feature, so it is not useable in this case. – Peter Krassoi Jul 14 '16 at 12:58