-
December 19th, 2007, 06:52 AM
#1
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
-
December 19th, 2007, 08:27 AM
#2
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
-
December 19th, 2007, 09:31 AM
#3
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.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|