相关疑难解决方法(0)

为什么字符串的行为类似于ValueType

在执行这段代码之后我感到困惑,其中字符串似乎表现得好像它们是值类型.我想知道赋值运算符是否运行像字符串的相等运算符之类的值.

这是我为测试此行为而执行的一段代码.

using System;

namespace RefTypeDelimma
{
    class Program
    {
        static void Main(string[] args)
        {
            string a1, a2;

            a1 = "ABC";
            a2 = a1; //This should assign a1 reference to a2
            a2 = "XYZ";  //I expect this should change the a1 value to "XYZ"

            Console.WriteLine("a1:" + a1 + ", a2:" + a2);//Outputs a1:ABC, a2:XYZ
            //Expected: a1:XYZ, a2:XYZ (as string being a ref type)

            Proc(a2); //Altering values of ref types inside a procedure 
                      //should reflect in the variable thats being passed …
Run Code Online (Sandbox Code Playgroud)

c# string

8
推荐指数
4
解决办法
1503
查看次数

标签 统计

c# ×1

string ×1