关于数组的困惑

时间:2017-02-04 17:54:53

标签: c# arrays

我正在尝试将数组的一部分复制到其他位置。这是宣言。

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

当我这样做时,

ALLOBJECTS[1]= ALLOBJECTS[0];

对任何一个所做的任何更改都会导致另一个更改。

根据我的理解,将指针设置为相同的地址( '复制'它,但不是我想要的)。

如何将指针ALLOBJECTS [0]中存储的内存复制到ALLOBJECTS [1]?

事情尝试了:

  • Array.Copy()(仍复制指针......)
  • 使用解除引用运算符(没有工作......)

3 个答案:

答案 0 :(得分:3)

您需要一个复制构造函数或使ObjectBasicFeatures成为一个struct(struct是值类型而class是引用类型) 然后你可以写:

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

另一个例子:

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; }
}

答案 1 :(得分:1)

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

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

enter image description here

答案 2 :(得分:0)

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

}

希望这有帮助。

如果您的Class ObjectBasicFeatures具有复杂属性,则应考虑深层复制功能