-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
Problems
There are two relevant cases, and both of them are related to nameof().
-
Consider the following code:
public static readonly string Sql = $"SELECT {nameof(ColumnX)} ..."; // moving to a constant public const string Sql = "SELECT " + nameof(ColumnX) + " ...";
Even though
nameofis a constant I have to fall back on older syntax here. -
Another (more insidious) example:
var sql = new StringBuilder(); sql.Append("SELECT * FROM " + nameof(C)); // vs sql.Append($"SELECT * FROM {nameof(C)}");
Even though approaches seem similar, the first one is a constant, while the second one does string allocation — defeating the purpose of a
StringBuilder. This can be partially mitigated by having aFormattableStringoverload onStringBuilder, butFormattableStringitself and its internal array would still need allocations.
Solution Discussion
I would like to evaluate string interpolation as a constant in certain contexts (that could only work if all values in it were constants). But I can't think of a simple syntax.
One option could be something like (const)$"A {nameof(B)} C", but I'm not too fond of it.