如何证明C#中字符串的不可变性?

在我上一次的c#访谈中,
我被要求证明C#字符串的不变性,我知道c#字符串的不变性是什么意思,但是有可能通过代码证明c#字符串的不变性吗? 我可以有一个示例代码片段吗? 提前致谢


我可以证明一个string不是不可变的。 我需要做的就是显示一些代码,这些代码会改变string ,如下所示:

using System;
using System.Runtime.InteropServices;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            const string test = "ABCDEF"; // Strings are immutable, right?
            char[] chars = new StringToChar {str = test}.chr;
            chars[0] = 'X';

            // On an x32 release or debug build or on an x64 debug build, 
            // the following prints "XBCDEF".
            // On an x64 release build, it prints "ABXDEF".
            // In both cases, we have changed the contents of 'test' without using
            // any 'unsafe' code...

            Console.WriteLine(test);

            // The following line is even more disturbing, since the constant
            // string "ABCDEF" has been mutated too (because the interned 'constant' string was mutated).

            Console.WriteLine("ABCDEF");
        }
    }

    [StructLayout(LayoutKind.Explicit)]
    public struct StringToChar
    {
        [FieldOffset(0)] public string str;
        [FieldOffset(0)] public char[] chr;
    }
}

现在,这是否应该被视为C#中的错误是另一回事。 :)(答案可能是FieldOffset应该被认为是unsafe - 上面的代码据称是safe ,因此string不应该是可变的。)

另外,我认为你可以合理地认为, string在精神上是不变的,即使在假定的安全代码中存在违反其不可变性的愚蠢的边缘案例。


是的,可以使用ObjectIDGenerator Class来证明c#字符串的不变性。

下面的答案取自Stringm在String#

实际上,ObjectIDGenerator将为我们在程序中创建的实例返回一个唯一的整数值。借助这个类,我们可以检查是否为string和stringbuilder上的各种操作创建了新实例。考虑以下程序

using System;
using System.Text;
using System.Runtime.Serialization;

class Program
{
  static void Main(string[] args)
  {
    ObjectIDGenerator idGenerator = new ObjectIDGenerator();
    bool blStatus = new bool();
    //just ignore this blStatus Now.
    String str = "My first string was ";
    Console.WriteLine("str = {0}", str);
    Console.WriteLine("Instance Id : {0}", idGenerator.GetId(str, out blStatus));
    //here blStatus get True for new instace otherwise it will be false
    Console.WriteLine("this instance is new : {0}n", blStatus);
    str += "Hello World";
    Console.WriteLine("str = {0}", str);
    Console.WriteLine("Instance Id : {0}", idGenerator.GetId(str, out blStatus));
    Console.WriteLine("this instance is new : {0}n", blStatus);
    //Now str="My first string was Hello World"
    StringBuilder sbr = new StringBuilder("My Favourate Programming Font is ");
    Console.WriteLine("sbr = {0}", sbr);
    Console.WriteLine("Instance Id : {0}", idGenerator.GetId(sbr, out blStatus));
    Console.WriteLine("this instance is new : {0}n", blStatus);
    sbr.Append("Inconsolata");
    Console.WriteLine("sbr = {0}", sbr);
    Console.WriteLine("Instance Id : {0}", idGenerator.GetId(sbr, out blStatus));
    Console.WriteLine("this instance is new : {0}n", blStatus);
    //Now sbr="My Favourate Programming Font is Inconsolata"
    Console.ReadKey();
  }
}

输出将看起来像这样 在这里输入图像描述

当str与“Hello World”串联时,字符串的实例id从1变为2,同时追加操作后sbr的实例ID与3保持一致。 这告诉所有关于易变性和不变性。 blStatus变量指示实例是否是新的。

您可以从以下网址找到有关该主题的完整文章:http://dotnetmob.com/csharp-article/difference-string-stringbuilder-c/


一个简单的例子:

string str = "test";string str2 = str;Console.WriteLine(str2);

//输出:测试

str = "DDD";Console.WriteLine(str2);

//输出:测试

试试这个代码,如果字符串是可变的,“str2”应该是“DDD”。

链接地址: http://www.djcxy.com/p/91815.html

上一篇: How to Prove Immutabiltiy of String in C#?

下一篇: Rails using Symbol vs String as key in params hash