Home  >  Article  >  Backend Development  >  Teach you step by step how to use pointers in C#

Teach you step by step how to use pointers in C#

烟雨青岚
烟雨青岚forward
2020-06-30 13:21:507474browse

Teach you step by step how to use pointers in C#

Teach you step by step how to use pointers in C

#C# is an interpreted language in which pointers are encapsulated, so users The pointer of an object cannot be called directly. But when using C# to call a C/C DLL, it is often the case that the function parameters or return values ​​are pointers. In this case, pointers need to be manipulated.

To use pointers in C#, you need to answer the following questions first:

1. What types of pointers are provided by C

#C# provides The pointer is an IntPtr or UIntPtr, a platform-specific type used to represent a pointer or handle. Therefore, IntPtr can be used to represent pointers or handles and is a platform-specific type.

1.1 Where is IntPtr used?

(1) When C# calls WIN32 API

(2) When C# calls DLL written in C/C (In fact, it is the same as 1, but this is usually used when we cooperate with others to develop)

1.2 How to use IntPtr

For example, there is a function prototype defined as:

DllDemoAPI DLLGen* DLLGen_Create(HWND hWnd);

Then when we quote in C#, we must write like this:

[DllImport("DLLGen.dll", EntryPoint = "DLLGen_Create", CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr DLLGen_Create(IntPtr hWnd);

This involves the correspondence between C# types and C types. The commonly used ones are as follows:

(1) void * can be directly converted to IntPtr.

(2) char * corresponds to string type in C#.

2. What is the difference between managed and unmanaged

The automatic allocation and recycling mechanism of memory allocation space in C# is implemented using managed memory. The so-called managed memory Memory is where the program is responsible for allocating pointer memory and determining whether the pointer needs to be released by counting the number of references to the pointer.

Unmanaged refers to allocating pointer memory to an unmanaged memory area. The pointer allocated here needs to allocate memory by itself and recycle the memory by itself.

3. How to use pointers as in C/C

In C#, you can use unsafe statements to operate pointers. For example

unsafe
{
    int *ptr = new int[100];
    for (int i = 0; i < 100; i++)
    {
        *(ptr+i) = i;
    }
    delete[] ptr;
}

In the unsafe module, pointers can be used in the c/c way.

Note: When using unsafe modules, check the "Allow the use of unsafe modules" option in the C# project properties.

Thank you everyone for reading, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/menjiawan/article/details/48677455

Recommended tutorial: "C Language"

The above is the detailed content of Teach you step by step how to use pointers in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete