Distinguish a string literal from a string C#

前端 未结 2 438
孤城傲影
孤城傲影 2021-01-26 05:43

I want to do something like

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

With the obvious return value. Is it possible?

2条回答
  •  無奈伤痛
    2021-01-26 06:01

    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?

提交回复
热议问题