C # Unsafe code (1)

Source: Internet
Author: User

The main reference is C # Language specification Version 3.0.

Introduction

C # is the main development language on the. NET platform, and unlike the classic C + +, the code written by C # is managed code, and the GC manages the memory, eliminating the new/delete annoyance. However, C # provides unsafe code for certain specific requirements, such as the underlying operating system interface, access to memory-mapped devices, or implementation of time-demanding algorithms.

Unsafe context

Unsafe code can only be written in an unsafe context.

You can use the unsafe modifier to modify:

class, struct, interface, or delegate

field, method, property, event, indexer, operator, instance constructor, destructor, or static constructor

unsafe-statement-block

Type of pointer

In an unsafe context, the pointer type is the same as a reference type or a value type. However, pointer types can be used in typeof outside the unsafe context, although it is not safe to do so.

Type t = typeof (int32*); the return is system.int32*.

Pointer types are represented by unmanaged types or void plus *.

pointer-type:
unmanaged-type   *
void   *

unmanaged-type:
type

The pointer type before * is called the reference type of the pointer type. It indicates the type of variable that the value of the pointer variable points to.

An unmanaged type is not a reference type and contains no members of any nested reference type.

An unmanaged type is one of the following:

sbyte, Byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.

· Any enum-type.

· Any pointer-type.

· Any user-defined struct-type that contains fields of unmanaged-types.

Example:

Example Description
byte* Pointer to byte
char* Pointer to Char
int** Pointer to pointer to int
Int*[] Single-dimensional array of pointers to int
void* Pointer to unknown type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.