Reference into an array

  • Thread starter Thread starter Tom Jones
  • Start date Start date
T

Tom Jones

Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I should
be able to use this reference to obtain the value of the 3rd element even
when the value stored there changes.

Thanks,

TJ
 
Tom Jones said:
Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I should be able to use this
reference to obtain the value of the 3rd element even when the value stored there changes.

For Value types like int you will need to Re-access the array every time since value types are
copied when you read them:
int x = myArray[2]; // copies the third element to x

Bill
 
Tom Jones said:
Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I
should be able to use this reference to obtain the value of the 3rd
element even when the value stored there changes.

Thanks,

TJ

myArray[2] ?

or are you asking for a direct pointer to the 3rd element rather than using
the array index. The latter isn't possible without using unsafe code and
unsafe code will mean pinning the array.
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:[email protected]...
Tom Jones said:
Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I
should be able to use this reference to obtain the value of the 3rd
element even when the value stored there changes.

Thanks,

TJ

myArray[2] ?

or are you asking for a direct pointer to the 3rd element rather than
using the array index. The latter isn't possible without using unsafe code
and unsafe code will mean pinning the array.
Regards

Hi Richard,

Thanks for the reply. Yes I am talking about a "pointer". I know that I
can do this:

unsafe static void Main(string[] args)
{
int[] myArray = { 1, 2, 3, 4, 5 };

fixed(int* pi = &myArray[3])
{
Console.WriteLine(*pi); // displays "4"
myArray[3] = 6;
Console.WriteLine(*pi); // displays "6"
}
}

Why is creating a reference to a reference considered so "bad" that I have
to resort to this type of thing?

Thanks,
BT
 
Tom Jones said:
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:[email protected]...
Tom Jones said:
Hi,

Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };

How can I define a reference to say the 3rd element of that array? I
should be able to use this reference to obtain the value of the 3rd
element even when the value stored there changes.

Thanks,

TJ

myArray[2] ?

or are you asking for a direct pointer to the 3rd element rather than
using the array index. The latter isn't possible without using unsafe code
and unsafe code will mean pinning the array.
Regards

Hi Richard,

Thanks for the reply. Yes I am talking about a "pointer". I know that I
can do this:

unsafe static void Main(string[] args)
{
int[] myArray = { 1, 2, 3, 4, 5 };

fixed(int* pi = &myArray[3])
{
Console.WriteLine(*pi); // displays "4"
myArray[3] = 6;
Console.WriteLine(*pi); // displays "6"
}
}

Why is creating a reference to a reference considered so "bad" that I have
to resort to this type of thing?

it's not that this is "bad". just when you program in a managed
environment, there are certain rules you need to follow. with the presence
of a garbage collector, you just can't randomly hold pointers to memory
locations because things get moved around all the time. that's the tradeoff,
you gain certain features in the managed world by giving up other features.
 
Tom,
Say I have a managed array:
int[] myArray = { 1, 2, 3, 4, 5 };
How can I define a reference to say the 3rd element of that array?

How would you use such as thing if it was supported? Can't you just
define your own type that encapsulates the array reference and an
index? Something like

class ArrayElement<T>
{
private T[] _array;
private int _idx;

public ArrayElement(T[] array, int idx)
{
Debug.Assert(array != null);
_array = array;
_idx = idx;
}

public T Value
{
get { return _array[_idx}; }
set { _array[_idx] = value; }
}

// Add conversion operators and stuff if you want to
// make it a bit more transparent.
}

ArrayElement<int> third = new ArrayElement<int>(myArray, 2);
third.Value = 42;


Mattias
 
Back
Top