性能 - 通过值或引用? [英] Properties - by value or reference?

查看:126
本文介绍了性能 - 通过值或引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这暴露了一个的ArrayList 以下公共财产:

I've got the following public property which exposes an Arraylist:

public ArrayList SpillageRiskDescriptions
        {
            get
            {
                return _SpillageRiskDescriptions;
            }
            set
            {
                _SpillageRiskDescriptions = value;
            }
        }

在其他地方我打电话

Elsewhere I'm calling

SpillageRiskDescriptions.Add("VENTILATE AREA");
SpillageRiskDescriptions.Add("DO NOT ALLOW SPILLAGE TO ENTER MAINS");

这些似乎(通过属性)来将元素添加到私人的ArrayList _SpillageRiskDescriptions 而我会一直期待这会导致一个问题。所以我是正确的思想,属性返回一个引用以原始变量,而不是通过的价值传递给它?这是因为的ArrayList 是引用类型?将在同一个发生在一个 INT (比如?)

These seem to be adding elements to the private ArrayList _SpillageRiskDescriptions (through the property) whereas I would've expected this to cause a problem. Therefore am I correct in thinking that properties return a reference to the original variable and not passing it by value? Is this because ArrayList is a reference type? Will the same happen with an int (for example?)

推荐答案

从技术上讲,它总是由价值,但你要明白什么是被通过。因为它是一个引用类型,你传递一个参考回(但按价值计算)。

Technically it's always by value, but you have to understand what is being passed. Since it's a reference type, you are passing a reference back (but by value).

希望是有道理的。你总是通过的结果回值,但如果类型是引用您传递基准回值,这意味着你可以改变的对象,而不是哪个对象是指。

Hope that makes sense. You always pass the result back by value, but if the type is a reference you are passing the reference back by value, which means you can change the object, but not which object it refers to.

这篇关于性能 - 通过值或引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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