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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
