简体   繁体   中英

indexes in c # work with memory

I began to parse the topic indexers in c # and ran into a problem while running a test task:

Index.cs:

class Index
{
    double[] arr;
    public int Length
    {
        get;
        set;
    }
    public double this[int x]
    {
        get { return arr[x]; }
        set
        {
            arr[x] = value;
        }
    }
    public Index(double[] arr1, int x, int y)
    {    
        Length = y;    

        arr = arr1;

        for (int i = x; i <= y; i++)
        {
            if (i == 0)
            {
                arr[i] = arr1[i + 1];
            }
            else
            {
                arr[i - 1] = arr1[i];
            }
        }
    }
}

Program.cs

static void Main(string[] args)
{
    double[] array = { 1, 2, 3, 4 };        
    var indexer0 = new Index(array, 1, 2);
    Console.WriteLine(indexer0.Length);
    Console.WriteLine("Result = 2,3");       
    var indexer1 = new Index(array, 1, 2);
    Console.WriteLine(indexer1[0]);
    Console.WriteLine(indexer1[1]);
    Console.WriteLine("Copy 25");        
    var indexer2 = new Index(array, 1, 2);
    var indexer3 = new Index(array, 0, 2);
    indexer2[0] = 25;
    Console.WriteLine(indexer3[1]);
    Console.WriteLine("Array");
    foreach(var item in array)
    {
        Console.WriteLine(item);
    }

    Console.ReadKey();
}

As you can see after calling indexer1, I want the array values ​​to change to 2 and 3, and I get 3.3 (I understand that this is due to the fact that when I call indexer0 I change the values ​​to 2.3, I then work with array 2,3,3,4, I can fix this by writing the values ​​to a temporary variable, but then I can not copy the value from indexer2 to indexer3 (the required value is 25.) Please help me deal with two of these problems.

The problem with your implementation is that you are not making a copy of the original array. This creates a second reference to the same array

 arr = arr1;

so modifications inside the loop, like this one,

arr[i] = arr1[i + 1];

are moving around and overwriting elements of the original array.

Fix this by allocating a new array, and copying the content of the original one into it:

public int Length { get { return arr.Length; } }
public Index(double[] arr1, int x, int y) {
    arr = new double[y];
    Array.Copy(arr1, x, arr, 0, y);
}

Note that your Index will have a copy of the original array. If you want a "window" into the original array, not a copy (ie have changes to Main 's array be visible through index1 , index2 , etc. store arr unchanged, and also store the initial index in the private variable. You can now change the indexer implementation to do "index translation", ie subtracting the stored x from index to get the correct index:

class Index {
    private readonly double[] arr;
    private readonly int offset; // set this to x in the constructor
    public int Length { get { return arr.Length; } }
    public double this[int idx] {
        get { return arr[idx+offset]; }
        set { arr[idx+offset] = value; }
    }
    public Index(double[] arr1, int x, int y) {
        arr = arr1;
        offset = x;
    }
}

Demo.

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