Prove String是C#中的引用类型

时间:2014-10-08 03:07:41

标签: c#-2.0

虽然我知道字符串是引用类型,但我现在可以证明它。因为如果我将变量test1作为字符串并将其设置为某个值,则将其他变量命名为test2并将test2与test1 varable一起使用。所以如果string是引用类型,那么变量test1的地址将与变量test2一致。所以如果我在tet2变量中设置了一些值,它将反映在变量test1中,但它没有发生。

另一个例子是如果我们在函数中传递两个字符串变量并且此函数中的更改值将反映在调用函数中,但它没有发生,请参阅示例: - 并举例说明我可以证明字符串是引用类型。但是在case类对象和字符串变量中的行为是不同的,请再次看这个例子。我已经编辑了

private void button1_Click(object sender, EventArgs e)
    {
        string test1 = string.Empty;
        string test2;

        String test3 = new String(new char[10]);

        test1 = "hemant";
        test2 = test1;


        test2 = "Soni";

        //becuase if string is reference type then after asign value "soni"
        // in test2, test1 also show value "soni" instead of value "hemant"

        Console.WriteLine("test1:" + test1); // but displaying "hemant", while shoul display "soni"
        Console.WriteLine("test2:" + test2); // while displaying "soni"

        test3 = "soni";

        // if string is reference type then after calling this function
        //value of variables test1 and test3 should be changed.
        testfn(test1, test3);
        Console.WriteLine("test1:" + test1);// but stile displaying "hemant" instead of "hemant New"
        Console.WriteLine("test3:" + test3);// but stile displaying "soni" instead of "soni New"

        // should be true, because if string reference type then address of both variable should be same.
        bool check = test1 == test2;

        clsTest obj = new clsTest();
        obj.x1 = "Hemant";
        obj.x2 = "Soni";

        Console.WriteLine("obj.x1:" + obj.x1); //"Hemant";
        Console.WriteLine("obj.x2:" + obj.x2);//"Soni";
        callFn(obj);
        //after calling  value has been changed of this object, but in string type its not happed like this
        Console.WriteLine("obj.x1:" + obj.x1); //"Hemant New";
        Console.WriteLine("obj.x2:" + obj.x2);//"Soni New";


    }
     public class clsTest
    {
        public string x1;
        public string x2;
    }

    void callFn(clsTest obj)
    {
        obj.x1 = "Hemant New";
        obj.x2 = "Soni New";
    }
    void testfn(string x, String y)
    {
        x = "Hemant New";
        y = "Soni New";
    }

2 个答案:

答案 0 :(得分:1)

String是引用类型,但它也是不可变的。意思是,您无法更改字符串对象的值,只能将其替换为对另一个字符串的引用。因此,当您将字符串传递给函数并分配参数时,您只需更改x参数的引用,而不更改x处的值。要更改x处的引用,您必须在参数上使用ref或out关键字。对于C#中的任何类型的对象也是如此。

class Example {
    public string Value;
}

void Foo(Example x, ref Example y) {
  x.Value = "mutated"; // mutating the reference
  x = new Example { Value = "new reference" }; // only changes local variable

  y.Value = "mutated"; // mutating the reference
  y = new Example { Value = "new reference" }; // changes the references y is pointing to
}


var x = new Example { Value = "x" };
var y = new Example { Value = "y" };
Foo(x, ref y);

Console.WriteLine(x.Value); // mutated
Console.WriteLine(y.Value); // new reference

但由于字符串是不可变的(它不能被改变),更改作为参数传入的值的唯一方法是使用ref或out关键字。

答案 1 :(得分:0)

ref关键字添加到testfn。

void testfn(ref string x, ref String y)

这将直接引用该对象。

MSDN ref page