String对象是真的被引用? [英] String object is really by reference?

查看:179
本文介绍了String对象是真的被引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是学习(新手).NET和我有些疑惑。

从读一本书的例子我才知道,字符串是对象,然后引用类型。

所以,我做了这个测试,结果是不同的我所期待的:

我真的很好奇,这是一个例外,因为串是特殊类型的?

 类节目
{
    静态无效的主要(字串[] args)
    {
        为SByte一个= 0;
        字节B = 0;
        Int16的C = 0;
        INT32 D = 0;
        Int64的E = 0;
        字符串s =;
        例外EX =新的异常();

        [对象]类型= {A,B,C,D,E,S,EX};

        // C#
        的foreach(在类型对象o)
        {
            字符串类型;
            如果(o.GetType()。IsValueType)
                TYPE =价值型;
            其他
                TYPE =引用类型;
            Console.WriteLine({0}:{1},o.GetType(),类型);
        }

        //测试是否变化
        字符串str =我永远不会改变!

        Program.changeMe(STR);

        Console.WriteLine(STR);
    }

    公共静态字符串changeMe(字符串参数)
    {
        参数=我已经改变了你!;

        返回 ; //师妹测试
    }
}
 

输出:

  System.SByte:值类型
System.Byte:值类型
System.Int16:值类型
System.Int32的:值类型
System.Int64:值类型
System.String:参考类型
System.Exception的:引用类型
我永远也不会会改变!
 

解决方案

字符串确实是一个引用类型。然而,当你的主要方法调用changeMe(STR),.NET传递引用的副本为str为changeme在参数参数。 changeMe然后修改这个副本是指我已经改变了你!,但原来的STR引用仍然指向我永远不会改变。

作为引用类型意味着如果你改变了传递的字符串的状态的,调用者会看到这些变化。 (你不能这样对一个字符串,因为字符串是不可变的,但你可以把它做其他的引用类型,例如控制)。但是重新分配的参数不改变的的调用者传递的参数,即使这个值是一个参考

I have being studying (newbie) .NET and I got some doubts.

Reading from a book examples I learnt that String are object then Reference Type.

So, I did this test and the result was different what I expected:

I'm really curious, is this an exception because "string" are special types?

class Program
{
    static void Main(string[] args)
    {
        SByte a = 0;
        Byte b = 0;
        Int16 c = 0;
        Int32 d = 0;
        Int64 e = 0;
        string s = "";
        Exception ex = new Exception();

        object[] types = { a, b, c, d, e, s, ex };

        // C#
        foreach (object o in types)
        {
            string type;
            if (o.GetType().IsValueType)
                type = "Value type";
            else
                type = "Reference Type";
            Console.WriteLine("{0}: {1}", o.GetType(), type);
        }

        // Test if change
        string str = "I'll never will change!";

        Program.changeMe(str);

        Console.WriteLine(str);
    }

    public static string changeMe(string param)
    {
        param = "I have changed you!!!";

        return ""; // no return for test
    }
}

Output:

System.SByte: Value type
System.Byte: Value type
System.Int16: Value type
System.Int32: Value type
System.Int64: Value type
System.String: Reference Type
System.Exception: Reference Type
I'll never will change!

解决方案

String is indeed a reference type. However, when your Main method calls changeMe(str), .NET passes a copy of the reference to str to changeMe in the param argument. changeMe then modifies this copy to refer to "I have changed you!!!", but the original str reference still points to "I will never change".

Being a reference type means that if you changed the state of the passed string, the caller would see those changes. (You can't do this to a string because strings are immutable, but you can do it to other reference types e.g. Control.) But reassigning a parameter doesn't change the value the caller passed in that parameter, even if that value is a reference.

这篇关于String对象是真的被引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆