Pointers

  • Thread starter Thread starter Marc
  • Start date Start date
Marc,

C# does support pointers in unsafe code. Also, if you are making calls
to unmanaged code, pointers are represented in with the IntPtr structure (in
safe code), and can be manipulated through various API calls, as well as
calls to the methods on the Marshal class.

Hope this helps.
 
c# supports pointers in unsafe context only

unsafe public void Foo(MyClass a)
{
fixed(int * ptr = &a.x)
{
//use the pointer here
}
}

Hoever, unsafe context has to be avoided because the code inside is not
verifiable.

HTH
B\rgds
100
 
Marc said:
Does C# support pointers in any way, or are references in functions the only
way to go?
There is support for pointers.

You need to use the unsafe keyword, to make a block for the pointer.

unsafe {
fixed (int* pInt = (int*)intArray) {
// work with the pInt here
}
}

This will not be garbage collected.

You may want to think whether you really need pointers, as C# tries
to be safer than C/C++, and not allowing pointers is one of the way to
try to help.
 
Back
Top