-
Ref to value type
Is there any way to get a reference to a value type variable as was possible in C+++ with the & operator ?
e.g.
Code:
private int [] m_MyIntArray=new int[1000];
private void MyFunc(int _nMyParam1,int _nMyParam2,int _nMyParam3)
{
m_MyIntArray[_nMyParam1 * 10 - _nMyParam2 + _nMyParam3]=_nParam1 *5;
if ((m_MyIntArray[_nMyParam1 * 10 - _nMyParam2 + _nMyParam3] > 100) &&
(m_MyIntArray[_nMyParam1 * 10 - _nMyParam2 + _nMyParam3] < 200))
{
.. do something
}
}
Instead of this, I'd like to store the indexed array element in some temporary reference to the element. This for not having to m_MyIntArray[_nMyParam1 * 10 - _nMyParam2 + _nMyParam3] all the time ;)
-
Re: Ref to value type
You can use pointers in C# just like you did in C++. You just have to check the option in your project to allow unsafe code.
Then take a look at this: http://www.c-sharpcorner.com/UploadF...sInCSharp.aspx
-
Re: Ref to value type
Code:
int index = _nMyParam1 * 10 - _nMyParam2 + _nMyParam3;
private void MyFunc(int _nMyParam1,int _nMyParam2,int _nMyParam3)
{
m_MyIntArray[index]=_nParam1 *5;
if ((m_MyIntArray[index] > 100) &&
(m_MyIntArray[index] < 200))
{
.. do something
}
}
I'd highly recommend you don't use pointers unless absolutely required. In this case, they're not.