Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc

    #1

    Pointers

    Does C# support pointers in any way, or are references in functions the only
    way to go?


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Pointers

    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.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Marc" <macfreak2000@h otmail.com> wrote in message
    news:%23I7zJQ1j DHA.2232@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Does C# support pointers in any way, or are references in functions the[/color]
    only[color=blue]
    > way to go?
    >
    >[/color]


    Comment

    • 100

      #3
      Re: Pointers

      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" <macfreak2000@h otmail.com> wrote in message
      news:%23I7zJQ1j DHA.2232@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > Does C# support pointers in any way, or are references in functions the[/color]
      only[color=blue]
      > way to go?
      >
      >[/color]


      Comment

      • James Black

        #4
        Re: Pointers

        Marc wrote:
        [color=blue]
        > Does C# support pointers in any way, or are references in functions the only
        > way to go?[/color]
        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.

        Comment

        Working...