我有以下活动课。我有一个与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();
        }
    }

最佳答案

它将进行浅表复制,换言之,它将仅复制参考。如果要进行深拷贝(或克隆),则将IClonable接口添加到您的类中。这将迫使您添加一个新的Clone方法,该方法实现确切的深度复制逻辑

关于c# - 属性参数是按值传递的?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2313657/

10-14 05:24