引用类型的行为就像在VB.NET值类型 [英] Reference types behaves like value type in VB.NET

查看:238
本文介绍了引用类型的行为就像在VB.NET值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管串是引用类型,在VB.NET中,我们只有使用引用类型的,如果我们传递一个参数作为ByRef的效果。因此,与C#甚至引用类型在VB.NET行为就像默认值类型。为什么会出现这种差异?

Even though "String" is a reference type, in VB.NET, we only get the effect of using of a reference type if we pass a parameter as ByRef. So, unlike C# even reference types in VB.NET behaves like value type by default. Why is there this kind of difference?

推荐答案

如果你想了解使用字符串作为一个例子,那么你会混淆自己大的引用类型和价值类型及其在VB.NET和C#.NET差异时间。

If you are trying to understand reference types and value types and their differences in VB.NET and C#.NET using string as an example then you will confuse yourself big time.

正如大卫所说,字符串是引用类型,但他们是特殊的,那就是不可变的。这意味着,一旦你在参考地址创建一个字符串,则不能对其进行修改。如果你尝试这样做,那么.NET将保存修改后的串上不同的地址,并开始指向新的地址。旧的地址将作为垃圾收集一段时间。

Just as David mentioned, strings are reference types, but they are special, that is, immutable. Which means that once you have created a string on a reference address you cannot modify them. If you try to do that then .NET will store your modified string on a different address and start pointing to the new address. The old address will get garbage collected over a period of time.

例如

string str = "new string"; //One address space
str = "modified" + str; //Different address and not same address as above

此外,当你传递一个字符串参考的方法和修改字符串中的方法,实际上它只是指向一个不同的参考,但它让你感觉,实际参考的更新。

Moreover, when you are passing a string by reference to a method and modifying the string in the method, it is actually just pointing to a different reference, but it makes you feel that the actual reference is updated.

我希望这阐明你的问题有点。

I hope this clarifies your question a little.

这篇关于引用类型的行为就像在VB.NET值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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