zoukankan      html  css  js  c++  java
  • C# 可否对内存进行直接的操作?

    可以,用 unsafe。用的时候记得在项目属性(Properties)->生成(Build)->常规(General)中钩上允许不安全代码 (Allow unsafe code)。
    否则会出现这个错误:Unsafe code may only appear if compiling with /unsafe。

    // compile with: /unsafe

    using System;
    class UnsafeTest
    {
    // Unsafe method: takes pointer to int:
    unsafe static void SquarePtrParam(int* p)
    {
    *p *= *p;
    }

    unsafe static void Main()
    {
    int i = 5;
    // Unsafe method: uses address-of operator (&):
    SquarePtrParam(&i);
    Console.WriteLine(i);
    }
    }
    // Output: 25

    复制代码
    // compile with: /unsafe
    
    using System;
    class UnsafeTest
    {
        // Unsafe method: takes pointer to int:
        unsafe static void SquarePtrParam(int* p)
        {
            *p *= *p;
        }
    
        unsafe static void Main()
        {
            int i = 5;
            // Unsafe method: uses address-of operator (&):
            SquarePtrParam(&i);
            Console.WriteLine(i);
        }
    }
    // Output: 25
    复制代码
    复制代码
    // compile with: /unsafe
    
    using System;
    class UnsafeTest
    {
        // Unsafe method: takes pointer to int:
        unsafe static void SquarePtrParam(int* p)
        {
            *p *= *p;
        }
    
        unsafe static void Main()
        {
            int i = 5;
            // Unsafe method: uses address-of operator (&):
            SquarePtrParam(&i);
            Console.WriteLine(i);
        }
    }
    // Output: 25
    复制代码
  • 相关阅读:
    MVC框架及应用
    《架构之美》三
    《架构之美》二
    深度学习之多层感知器
    架构之美
    质量属性之淘宝案例分析
    配置cocos相关问题
    3-5
    web文本框之内容提示
    【LeetCode】024. Swap Nodes in Pairs
  • 原文地址:https://www.cnblogs.com/binyao/p/4891182.html
Copyright © 2011-2022 走看看