Search code examples
c#.netstringstring-literals

Distinguish a string literal from a string C#


I want to do something like

IsItAStringLiteral("yes")
var v = "no";
IsItAStringLiteral(v)

With the obvious return value. Is it possible?


Solution

  • You can use the string.IsInterned method to determine whether a given string is in the .NET intern pool. All string literals are automatically added to the intern pool before the application begins running.

    Of course, that won't help you with your exact question. Variable 'v' will reference a string literal, so it too will appear in the intern pool. Why do you need such functionality?