Inline Constant Strings #8088
-
local constants are wonderful, they allow me to keep my value close to its use case. For most types, the versatility of the local constants is sufficient. However I would propose the ability to declare an inline const string. For example. I have a usecase where I am doing a db query against a local sqlite database to provide metrics and the query looks something like this.
And this is done several times. Due to this being done inside a synchronous action used by the CreateObservableGuage method, I wrote a local function that looks something like this.
Now my calls to CreateObservableGuage look like this.
Ideally I would like it to be something. more like this however but using an inline const.
Now strings already have some modifiers you can add to them such as verbatim
This would allow me to do this for example
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
The compiler is already capable of treating simple string literal concatenation or interpolation as a constant, without any additional syntax: const string sql1 = "SELECT COUNT(1) AS Value FROM " + nameof(DbContext.MyTable);
const string sql2 = $"SELECT COUNT(1) AS Value FROM {nameof(DbContext.MyTable)}"; |
Beta Was this translation helpful? Give feedback.
Those are already literals/constants, though. Why do you need a different syntax?
These produce identical IL byte code: