C# 可否对内存进行直接的操作?
2015-10-19 10:37
519 查看
可以,用 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
否则会出现这个错误: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
相关文章推荐
- c#子线程调用主线程控件
- AX2009 C#客户端通过Web service批量审核工作流(一)
- (原创)c#学习笔记03--变量和表达式04--表达式04--命名空间
- 猴子选大王——CSharp数组
- C#面向对象设计模式纵横谈——3.Abstract Factory 抽象工厂(创建型模式)
- C#读写(OFFICE 2013)excel表格(续)
- (原创)c#学习笔记03--变量和表达式04--表达式03--运算符的优先级
- C#运算符重载和索引器
- (原创)c#学习笔记03--变量和表达式04--表达式02--赋值运算符
- (原创)c#学习笔记03--变量和表达式04--表达式01--数学运算符
- c# 输出一个数组
- c# 窗体编程注意事项
- C# 6.0 (C# vNext) 的新功能:Exception-Handling Improvements
- C#读写(OFFICE 2013)excel表格
- C#中的readonly
- C#学习日记25---匿名方法 与 Func委托 与 lambda表达式
- C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP (转)
- C#属性
- C#创建COM组件
- C#学习之操作excel表格