C # unsafe code

Source: Internet
Author: User

C # allows pointer variables to be used in a function when a block of code uses the unsafe modifier tag.

Unsafe or unmanaged code refers to a block of code that uses a pointer variable.

The following example illustrates the use of a Pointer when the unsafe modifier is used in C #:

Using System;namespace unsafecodeapplication{    class program    {        static unsafe void Main (string[] args)        {            int var =;            int* p = &var;            Console.WriteLine ("Data is: {0}",  var);            Console.WriteLine ("Address is: {0}",  (int) p);            Console.readkey ();}}}    

In order to compile unsafe code, you must switch to the command-line compiler to specify the /unsafe command line.

For example, to compile a program named Prog1.cs that contains unsafe code, you would enter a command on the command line:

/ unsafe Prog1 . CS

If you are using the Visual Studio IDE, you need to enable unsafe code in the project properties.

The steps are as follows:

    • Open Project Propertiesby double-clicking the Properties node in Resource Manager (Solution Explorer).
    • Click the Build tab.
    • Select the option "Allowunsafe code".


can refer to http://www.w3cschool.cc/csharp/csharp-unsafe-codes.html




C # unsafe code

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.