There are variables of the Form1.ValueSendMessages.ChatList[i] when assigning it to another variable SetData.MainData and, later, editing the ModificationUserData property of the variable SetData.MainData , why this property is replaced as in Form1.ValueSendMessages.ChatList[i].ModificationUserData and in SetData.MainData.ModificationUserData .

Why it happens?

 ChatDataMessages SetData = new ChatDataMessages(); if (_Type == "Chat" || _Type == "GetUserData") { SetData.MainData = Form1.ValueSendMessages.ChatList[_Index]; SetData.MainData.TupeMessage = _Type; SetData.MainData.SourceIP = GetLocalIPAddress(); //SetData.MainData.Message = System.Text.Encoding.UTF8.GetBytes(TextMessage); if (TextMessage != null) { SetData.MainData.Message = new byte[TextMessage.Count()]; SetData.MainData.Message = TextMessage; } SetData.MainData.LocalUser = Message.ChatSettingData.Name; SetData.MainData.LocalSID = Message.ChatSettingData.SID; SetData.MainData.ImagePath = string.Empty; SetData.MainData.Description = string.Empty; SetData.MainData.ModificationUserData = Message.ChatSettingData.ModificationUserData; } 

The variable Form1.ValueSendMessages.ChatList [i] .ModificationUserData is reassigned in one more place, but there definitely cannot be an error.

    1 answer 1

    Obviously, the elements of the reference type are stored in the list, and when assigning it, you do not copy the object, but only copy the link to this object, so after assignment, and Form1.ValueSendMessages.ChatList[i] and SetData.MainData refer to the same an object.

    Accordingly, when the property of this object changes, the changes will be noticeable in all variables that refer to it.

    • this is because of its static nature - SergD29
    • @ SergD29, did not understand the comment - Grundy