Question about array subtyping in C #

Back in the old days of C, you can use array subtypes for address storage in very useful ways. For example, you can declare an array as such.

This array represents an EEPROM image with 8-bit words.

BYTE eepromImage[1024] = { ... };

And later refer to this array as if it were really multi-dimensional storage

BYTE mpuImage[2][512] = eepromImage;

I am sure that my syntax is incorrect, but I hope you understand this idea.

In any case, this projected a two-dimensional image of what is actually a one-dimensional storage.

The two-dimensional projection is an EEPROM image when loaded into the MPU memory with 16-bit words.

In C, you can reference multidimensional memory and change storage values, and changed values ​​will be displayed in real (one-dimensional) storage almost in the same way as by magic.

Is it possible to do this with C #?

Our current solution uses multiple arrays and event handlers to synchronize the situation. Such works, but this is an additional complexity that we would like to avoid if there is a better way.

+5
source share
2 answers

You can wrap an array in a class and write the one-dimensional and two-dimensional properties of the indexer.

No checks, etc. it looks like this for a 10x10 array:

    class ArrayData
    {
        byte[] _data = new byte[100];

        public byte this[int x]          
        { 
            get { return _data[x]; } 
            set { _data[x] = value; }             
        }


        public byte this[int x, int y]
        {
            get { return _data[x*10+ y]; }
            set { _data[x*10 + y] = value; }
        }
    }
+9
source

Yes, but no.

, . , , .

.

+1

All Articles