When to use pointers in C#/.NET?

后端 未结 5 671
自闭症患者
自闭症患者 2020-12-02 07:16

I know C# gives the programmer the ability to access, use pointers in an unsafe context. But When is this needed?

At what circumstances, using pointers becomes inevi

5条回答
  •  情话喂你
    2020-12-02 07:55

    Pointers are an inherent contradiction to the managed, garbage-collected, environment.
    Once you start messing with raw pointers, the GC has no clue what's going on.

    Specifically, it cannot tell whether objects are reachable, since it doesn't know where your pointers are.
    It also cannot move objects around in memory, since that would break your pointers.

    All of this would be solved by GC-tracked pointers; that's what references are.

    You should only use pointers in messy advanced interop scenarios or for highly sophisticated optimization.
    If you have to ask, you probably shouldn't.

提交回复
热议问题