How to run unsafe code in “visual studio code”?

ぃ、小莉子 提交于 2019-12-24 08:31:52

问题


I am using Visual studio code and when I try to run an unsafe code it throws the following error ""message": Unsafe code may only appear if compiling with /unsafe"

and as in visual studio, it does not have option like project->properties.


回答1:


unsafe (C# Compiler Options)

  1. To set this compiler option in the Visual Studio development environment Open the project's Properties page.

    1. Click the Build property page.

    2. Select the Allow Unsafe Code check box.

  2. To add this option in a csproj file Open the .csproj file for a project, and add the following elements:

XML

  <PropertyGroup>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

Usage

Method level

unsafe static void FastCopy(byte[] src, byte[] dst, int count)  
{  
    // Unsafe context: can use pointers here.  
}  

Inline Block

...

unsafe  
{  
    // Unsafe context: can use pointers here.  
}

Class Level

public unsafe class Blah {}



回答2:


In the .csproj file, just add

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

to any <PropertyGroup> block.

No need to add anything to task.json.



来源:https://stackoverflow.com/questions/50636693/how-to-run-unsafe-code-in-visual-studio-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!