Quote Originally Posted by MontgomeryBurns View Post
Like CGKevin's class in first thread's reply?
No. What CGKevin tried to do was to meet your requirement for the method to return a byte[], however, as I already told you, in
C# you can't both meet the requirement to return a byte[], and the requirement for it to bee a shallow copy of the original array, at least not without doing some kind of boxing on the byte type - since byte is a value type. So, you need to redesign your app, but, fortunately, only slightly.

Take a look at this peace of code - it's just a quick example to demonstrate the idea:
Code:
class SubArray
    {
        private byte[] _srcArray = null;   // this is a reference to the *original* array
        private int _start = 0;
        private int _count = 0;

        public SubArray(byte[] array, int start, int count)
        {
            _srcArray = array;
            _start = start;
            _count = count;
        }

        public byte this[int index]
        {
            get
            {
                if (!CheckBounds(index))
                    throw new IndexOutOfRangeException();

                int i = index + _start;
                return _srcArray[i];
            }
            set
            {
                if (!CheckBounds(index))
                    throw new IndexOutOfRangeException();

                int i = index + _start;
                _srcArray[i] = value;
            }
        }

        public int Count
        {
            get { return _count; }
        }

        private bool CheckBounds(int index)
        {
            if (index < 0 || index >= _count)
                return false;

            return true;
        }
    }
Basically, the _srcArray variable is not a copy - it references (points to) the original array. The indexer enables you to work with a SubArray object pretty much the same way you'd work with an ordinary array. This code is just bare bones; so you'll want to add guard conditions and such, to make sure all the invariants are maintained.

You can then use the class like this:
Code:
        static void Main(string[] args)
        {
            byte[] testArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };

            SubArray sub = new SubArray(testArray, 2, 3);
            sub[0] = 255;
            sub[1] = 255;
            sub[2] = 255;

            foreach (byte b in testArray)
                Console.WriteLine(b);
        }
This prints out:
0
1
255
255
255
5
6
7


No copy is made, and the original array is updated.

You can go even further than that, and make the class implement the IList<byte> interface. This interface is implemented by the byte[] as well. Although it provides generic methods like Add(), Remove(), Insert(), and such, the array class simply throws an InvalidOperationException for these. It also implements them explicitly, so that they normally don't appear in IntelliSence, unless the interface itself is used to access the members. The only thing that would require a bit of work is the implementation of the GetEnumerator() methods, where you would need to provide a custom enumerator to iterate only through the desired segment. This method is used by the foreach statement, so it must be implemented in order for foreach to work. But the for loop will work normally with or without it. Once you've implemented the IList<byte> interface, you can replace all your byte[] method parameters with IList<byte>, and it will work the same as before (you won't have to change the most of the code, and you'll still be able to pass byte[] variables).
The up side is, if you later on decide to use some other list-like structure instead of a byte[] (like List<byte>), you can do that with minimum (or almost no) additional work.