简体   繁体   English

属性参数是按值传递的?

[英]Property parameter is pass by value?

I have following event class. 我有以下活动课。 I have a question related to the Property method set {userAccount = value;} It will make a copy of the userAccount object (deep copy?) or it will make a copy of the userAccount object reference (shallow copy?) Do I need to make a method in UserAccountInfo class to do value copy? 我有一个与Property方法set {userAccount = value;} ,它将创建userAccount对象的副本(深复制?),或者将复制userAccount对象引用的副本(浅复制吗?),我是否需要在UserAccountInfo类中做一个方法来做值复制?

class EvEndGetUserAccount
    {
        private UserAccountInfo userAccount;

        /// <summary>
        /// An event class for getting user account
        /// </summary>
        /// <param name="account"></param>
        public EvEndGetUserAccount(UserAccountInfo account)
        {
            userAccount = account;
        }

        /// <summary>
        /// Get/Set userAccount
        /// </summary>
        public UserAccountInfo UserAccount
        {
            get { return userAccount; }
            set { userAccount = value; }
        }

        /// <summary>
        /// returns the content of this EvEndGetUserAccount event.
        /// </summary>
        /// <returns>string represent of the EvEndGetUserAccount object</returns>
        public override string ToString()
        {           
            return userAccount.ToString();
        }
    }

It doesn't make a copy of the UserAccountInfo. 它不会复制UserAccountInfo。 It's a reference type, so your property is set as a reference to the existing object - not a new object. 这是引用类型,因此您的属性被设置为对现有对象(而不是新对象)的引用。

If you wish to create an entirely new object when you run the setter, you need to implement a Clone() method on UserAccountInfo that makes a deep copy. 如果您希望在运行setter时创建一个全新的对象,则需要在UserAccountInfo上实现一个Clone()方法,该方法可以进行深层复制。

It will make a shallow copy, in other words it will just copy the reference. 它将进行浅表复制,换言之,它将仅复制参考。 If you want to make a deep copy (or clone), add the IClonable interface to your class. 如果要进行深拷贝(或克隆),则将IClonable接口添加到您的类中。 It will force you to add a new Clone method that implements the exact deep-copying logic 这将迫使您添加一个新的Clone方法,该方法实现确切的深度复制逻辑

So first, some nomenclature: unless you use the ref keyword, C# is always pass-by-value. 首先,请注意一些术语:除非使用ref关键字,否则C#始终是按值传递的。 The distinction you're looking for is whether UserAccountInfo is a value type or a reference type. 您要查找的区别是UserAccountInfo是值类型还是引用类型。

Yes, your class is keeping a reference to the UserAccountInfo object. 是的,您的类正在保留对UserAccountInfo对象的引用。 If you want to change this, you should have it Clone() the userAccount object when it's passed in to both the setter and the constructor. 如果要更改此设置,则在将userAccount对象传递给setter和构造函数的同时,应该将其克隆()Clone()。 You'll also have to ensure that UserAccountInfo implements ICloneable. 您还必须确保UserAccountInfo实现ICloneable。

However, based on the limited context you provided, my guess is that you want to keep a reference, ie leave it like it is. 但是,基于您提供的有限上下文,我的猜测是您保留一个引用,即保持原样。

Here's a good link about parameter passing in C#: http://www.yoda.arachsys.com/csharp/parameters.html 这是有关在C#中传递参数的良好链接: http : //www.yoda.arachsys.com/csharp/parameters.html

It depends on what kind of type UserAccountInfo is. 这取决于UserAccountInfo是哪种类型。 If it is a reference type (typically a C# class ) then the = operator simply copies the reference to a single instance of the object. 如果它是引用类型(通常是C# class ),则=运算符仅将引用复制到该对象的单个实例。

If the type is a value type (eg declared using C# struct keyword or a primitive type such as int ) then it will copy the value of the object. 如果类型是值类型(例如,使用C# struct关键字声明的类型或原始类型(例如int )声明),则它将复制对象的值。 Since value types are usually immutable (meaning that you cannot modify the value), you cannot really recognize this by analyzing the behavior of a program (passing a reference and a value gives the same result for immutable data type). 由于值类型通常是不可变的(意味着您无法修改值),因此您无法通过分析程序的行为(通过引用和值给出不可变数据类型的相同结果)来真正识别出这一点。

If you need to create a copy, you can consider using the ICloneable interface. 如果需要创建副本,可以考虑使用ICloneable接口。 You can also clone objects using the MemberwiseClone method . 您还可以使用MemberwiseClone方法克隆对象。 In .NET the terms shallow and deep copy usually mean the following: 在.NET中,术语浅复制和深复制通常表示以下含义:

  • Shallow copy - creates a new instance and copies values of the fields of the original instance to the new one (this means that if the cloned object references some other object, the copy will reference the same object) 浅拷贝 -创建一个新实例,并将原始实例的字段值复制到新实例(这意味着,如果克隆的对象引用了其他对象,则副本将引用相同的对象)

  • Deep copy - clones the object and also clones all objects that the object references (and so on...). 深度复制 -克隆对象,还克隆对象引用的所有对象(依此类推...)。 This means that the whole object reference tree will be cloned. 这意味着将克隆整个对象参考树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM