What is the Proper approach for Constructing a PhysicalAddress object from Byte Array

Posted by Paul Farry on Stack Overflow See other posts from Stack Overflow or by Paul Farry
Published on 2010-04-14T04:24:35Z Indexed on 2010/04/14 4:53 UTC
Read the original article Hit count: 251

Filed under:
|

I'm trying to understand what the correct approach for a constructor that accepts a Byte Array with regard to how it stores it's data (specifically with PhysicalAddress)

I have an array of 6 bytes (theAddress) that is constructed once.

I have a source array of 18bytes (theAddresses) that is loaded from a TCP Connection.

I then copy the 6bytes from theAddress+offset into theAddress and construct the PhysicalAddress from it.

Problem is that the PhysicalAddress just stores the Reference to the array that was passed in. Therefore if you subsequently check the addresses they only ever point to the last address that was copied in.

When I took a look inside the PhysicalAddress with reflector it's easy to see what's going on.

public PhysicalAddress(byte[] address)
{
    this.changed = true;
    this.address = address;
}

Now I know this can be solved by creating theAddress array on each pass, but I wanted to find out what really is the best practice for this.

  1. Should the constructor of an object that accepts a byte array create it's own private Variable for holding the data and copy it from the original
  2. Should it just hold the reference to what was passed in.
  3. Should I just created theAddress on each pass in the loop

© Stack Overflow or respective owner

Related posts about .NET

Related posts about best-practices