question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Should be a compile time constant" warning when using nameof

See original GitHub issue

Example: Log.Information($"Hello {nameof(MyClass)}"); causes a “Message template should be a compile time constant” warning, but nameof expressions are evaluated at compile time.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
olshcommented, Nov 24, 2020

Well, after some research, I think that it’s not a mistake. Yes, the nameof expression is evaluated at compile-time, but the interpolation is not.

The compilation of this code fails

const string a = $"Hello {nameof(String)}";

But this code is valid

const string a = "Hello " + nameof(String);

You may say that this irrelevant in the context of Serilog templates. Because both interpolation and concatenation produce an immutable string, but that’s not completely true either.

Let’s look at IL for the code

Console.WriteLine($"Hello {nameof(String)}");

On .NET Core 3.1+ it creates a constant

IL_0000:  ldstr       "Hello String"
IL_0005:  call        System.Console.WriteLine
IL_000A:  ret       

But on old full .NET Framework string.Format() is called internally

IL_0000:  ldstr       "Hello {0}"
IL_0005:  ldstr       "String"
IL_000A:  call        System.String.Format
IL_000F:  call        System.Console.WriteLine
IL_0014:  ret         

And this is a performance issue https://nblumhardt.com/2014/09/how-not-to-parameterize-serilog-events/

Don’t do this. While never particularly good logging practise (the string concatenation occurs regardless of whether logging is enabled or not, wasting RAM and CPU cycles), with Serilog, this is strongly considered an anti-pattern.

So I’m not sure if we want to treat the interpolation as a constant.

Please let me know what you think.

P.S. There is a proposal for constant interpolated strings. https://github.com/dotnet/csharplang/issues/2951

0reactions
olshcommented, Feb 17, 2022

Well, it works if you target C# 10 C# 10 image

C# 9 image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is nameof() evaluated at compile-time?
Yes. nameof() is evaluated at compile-time. Looking at the latest version of the specs: The nameof expression is a constant.
Read more >
[Solved]-Is nameof() evaluated at compile-time?-C#
Yes. nameof() is evaluated at compile-time. Looking at the latest version of the specs: The nameof expression is a constant. In all cases,...
Read more >
Message template should be compile time constant-.net-core
Now for the real question: Why is there a warning? The answer is that string interpolation prevents structured logging. When you pass the...
Read more >
Class::property.name should be compile-time constant
Hi! I'm trying out Kotlin. Coming from a C# background, I already saw some people suggesting the nameof function. And I also saw...
Read more >
Miscellaneous attributes interpreted by the C# compiler
The Obsolete attribute marks a code element as no longer recommended for use. Use of an entity marked obsolete generates a warning or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found