简体   繁体   English

关于数组的困惑

[英]Confusion about arrays

I am trying to copy a part of an array to a different location. 我正在尝试将数组的一部分复制到其他位置。 Here is the declaration. 这是声明。

 public ObjectBasicFeatures[] ALLOBJECTS = new ObjectBasicFeatures[150];

When I do this, 当我这样做时

ALLOBJECTS[1]= ALLOBJECTS[0];

Any changes made to either one cause a change in the other one. 对一个进行任何更改都会导致另一个更改。

From what I understand it is setting the pointers to the same address (Which is 'copying' it, but not what I want). 据我了解,它是将指针设置为相同的地址( 正在 “复制”它,但不是我想要的)。

How can I copy the memory stored at pointer ALLOBJECTS[0] to ALLOBJECTS[1]? 如何将指针ALLOBJECTS [0]上存储的内存复制到ALLOBJECTS [1]?

Things tried: 尝试过的事情:

  • Array.Copy() (Still copied the pointers...) Array.Copy()(仍复制指针...)
  • Using the dereference operator (Didn't work...) 使用解引用运算符(无效...)

You need a copy constructor or make ObjectBasicFeatures a struct (struct is value type whereas class is a reference type) Then you could write: 您需要一个复制构造函数或将ObjectBasicFeatures构造为一个结构(struct是值类型,而class是引用类型),然后可以编写:

ALLOBJECTS[1]= new ObjectBasicFeatures(ALLOBJECTS[0]); ALLOBJECTS [1] =新的ObjectBasicFeatures(ALLOBJECTS [0]);

Another Example: 另一个例子:

class Program
{
    static void Main(string[] args)
    {
        var o1 = new ObjectBasicFeatures();
        var o2 = new ObjectBasicFeatures(o1);
        System.Diagnostics.Debug.Assert(!o1.Equals(o2));

    }
}

public class ObjectBasicFeatures
{
    public ObjectBasicFeatures()
    {
        MyProperty = 0;
    }

    /// <summary>
    /// copy constructor
    /// </summary>
    /// <param name="other"></param>
    public ObjectBasicFeatures(ObjectBasicFeatures other)
    {
        MyProperty = other.MyProperty;
    }

    public int MyProperty { get; set; }
}

To achieve this you need to create a constructor which takes input as its object and copies its values. 为此,您需要创建一个构造函数,该构造函数将输入作为其对象并复制其值。 But here is a catch. 但是这里有一个陷阱。 You need to do the same for all the classes you refer in ObjectBasicFeatures class and so on till the leaf node. 您需要对在ObjectBasicFeatures类中引用的所有类进行相同的操作,依此类推,直到叶子节点。 Below is a piece of code I tested with. 以下是我测试过的一段代码。

Please no the value of member (direct member of class) does not reflect in other(copied) element but the value of level2.member is updated in both the objects when you change it in one object 请不要让member(类的直接成员)的值不反映在其他(已复制)元素中,但是在一个对象中更改level2.member的值时,两个对象中的值都会更新

class Program
{
    static void Main(string[] args)
    {
        ObjectBasicFeatures[] ALLOBJECTS = new ObjectBasicFeatures[3];
        ALLOBJECTS[0] = new ObjectBasicFeatures("zero");
        ALLOBJECTS[1] = new ObjectBasicFeatures("one");
        ALLOBJECTS[2] = new ObjectBasicFeatures("two");
        ALLOBJECTS[1] = new ObjectBasicFeatures(ALLOBJECTS[0]);
        ALLOBJECTS[0].member = "Updated Value";
        ALLOBJECTS[0].level2Member.member = "Updated Level 2 Value";
        Console.WriteLine("At index 0 : " + ALLOBJECTS[0].member + ", Level2 : " + ALLOBJECTS[0].level2Member.member);
        Console.WriteLine("At index 1 : " + ALLOBJECTS[1].member + ", Level2 : " + ALLOBJECTS[1].level2Member.member);
        Console.ReadKey();
    }
}

public class ObjectBasicFeatures
{
    public string member;
    public Level2 level2Member; // This is to demonstrate that it will be updated in both the objects
    public ObjectBasicFeatures(string memberVal)
    {
        member = memberVal;
        level2Member = new Level2("Level 2 Value");
    }

    /// Constructor to copy member values.
    public ObjectBasicFeatures(ObjectBasicFeatures originalObject)
    {
        member = originalObject.member;
        level2Member = originalObject.level2Member;
    }

}

/// This class does not have a constructor to copy member values.
public class Level2 
{
    public string member;
    public Level2(string memberVal)
    {
        member = memberVal;
    }
}

Output of this will look like below 输出如下

在此处输入图片说明

ALLOBJECTS[1]= new ObjectBasicFeatures {
      PropertyName1=ALLOBJECTS[0].PropertyName1
      PropertyName2=ALLOBJECTS[0].PropertyName2

}

Hope this helps. 希望这可以帮助。

If your Class ObjectBasicFeatures has complex properties you should consider deep copy function 如果您的Class ObjectBasicFeatures具有复杂的属性,则应考虑使用深层复制功能

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

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