我在演示NoStringInterning时遇到问题
[assembly: System.Runtime.CompilerServices.CompilationRelaxations(System.Runtime.CompilerServices.CompilationRelaxations.NoStringInterning)]
class Program
{
static void Main(string[] args)
{
String s1 = "Friday";
String s2 = "Friday";
String s3 = "Friday";
String s4 = "Friday";
String s5 = "Friday";
String s6 = "Friday";
String s7 = "Friday";
String s8 = "Friday";
String s9 = "Friday";
// etc...
Console.WriteLine(Object.ReferenceEquals(s1, s2));
Console.WriteLine(Object.ReferenceEquals(s1, s3));
Console.WriteLine(Object.ReferenceEquals(s1, s4));
Console.WriteLine(Object.ReferenceEquals(s1, s5));
Console.WriteLine(Object.ReferenceEquals(s1, s6));
Console.WriteLine(Object.ReferenceEquals(s1, s7));
Console.WriteLine(Object.ReferenceEquals(s1, s8));
Console.WriteLine(Object.ReferenceEquals(s1, s9));
// etc...
我已经尝试使用.NET CLR 2、3、4 ..作为输出,我得到的只是一堆True。我期待一堆错误!
最佳答案
documentation指出该属性:
换句话说,它不会阻止编译器进行字符串实习,只是提供了一个不需要的提示。在这方面的文档很少,但是这似乎也是this MSDN forum post中的结论。
关于c# - CompilationRelaxations.NoStringInterning实际如何工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15601916/