Home Backend Development C#.Net Tutorial Teach you step by step how to use pointers in C#

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

Jun 30, 2020 pm 01:21 PM
c# pointer

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);
Copy after login

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);
Copy after login

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;
}
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

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

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

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

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

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

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

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.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

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.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

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

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

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 c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

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.

See all articles