简体   繁体   中英

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?

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. 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.

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. It will force you to add a new Clone method that implements the exact deep-copying logic

So first, some nomenclature: unless you use the ref keyword, C# is always pass-by-value. The distinction you're looking for is whether UserAccountInfo is a value type or a reference type.

Yes, your class is keeping a reference to the UserAccountInfo object. 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. You'll also have to ensure that UserAccountInfo implements 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

It depends on what kind of type UserAccountInfo is. If it is a reference type (typically a C# class ) then the = operator simply copies the reference to a single instance of the object.

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. 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. You can also clone objects using the MemberwiseClone method . In .NET the terms shallow and deep copy usually mean the following:

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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