Is there pointer in C# like C++? Is it safe?

后端 未结 5 1540
抹茶落季
抹茶落季 2020-12-08 18:20

I\'m writing an application that work with a tree data structure. I\'ve written it with C++, now i want to write it by C#. I use pointers for implementing the tree data stru

5条回答
  •  盖世英雄少女心
    2020-12-08 19:25

    YES. There are pointers in C#.

    NO. They are NOT safe.

    You actually have to use keyword unsafe when you use pointers in C#.

    For examples look here and MSDN.

    static unsafe void Increment(int* i)
    {
        *i++;
    }
    
    Increment(&count);
    

    Use this instead and code will be SAFE and CLEAN.

    static void Increment(ref int i)
    {
        i++;
    }
    
    Increment(ref count);
    

提交回复
热议问题