简单的问题,我导入一个DLL函数,参数都为int *。 当我尝试输入方法(0),我得到一个错误,说:int和INT *不能将
simple question, I import a DLL function and the parameter are int*. When I try to enter Method(0), I get an error which says: "int and int* can not convert".
那是什么意思?
这是一个指向int经典的C表示法
。每当类型后跟一个 *
,它指的类型作为的指针那个类型。在C#中,不像在C,你必须明确定义功能不安全
使用指针,除了启用的不安全 code。在项目属性。指针类型也不能直接互换的具体类型,这样类型的引用必须首先考虑。为了得到一个指向另一种类型,如int,在C#(或C和C ++的这个问题),您必须使用引用操作&安培;
(符号)在变量前面你希望得到一个指向:
That is classic C notation for a pointer to an int
. Whenever a type is followed by a *
, it denotes that type as a pointer to that type. In C#, unlike in C, you must explicitly define functions as unsafe
to use pointers, in addition to enabling unsafe code in your project properties. A pointer type is also not directly interchangeable with a concrete type, so the reference of a type must be taken first. To get a pointer to another type, such as an int, in C# (or C & C++ for that matter), you must use the dereference operator &
(ampersand) in front of the variable you wish to get a pointer to:
unsafe
{
int i = 5;
int* p = &i;
// Invoke with pointer to i
Method(p);
}
不安全code C#
下面是不安全code的一些关键文章,并在C#中使用指针。
Below are some key articles on unsafe code and the use of pointers in C#.
不安全的环境 指针类型 固定和移动的变量 指针转换 指针在EX pressions 的固定的声明 堆栈分配 动态内存分配 Unsafe contexts Pointer Types Fixed and movable variables Pointer conversions Pointers in expressions The 'fixed' statement Stack allocation Dynamic memory allocation