管理进化

c#有指针吗


C#是有指针的,只不过要先标记unsafe,在Rust语言中也是通过unsafe标记,指针在C#越来越重要,尤其是在.Net Core中使用指针也更多,在.Net Core是Unsafe类,有更多指针的操作。

C#是有指针的,只不过要先标记unsafe,在Rust语言中也是通过unsafe标记,指针在C#越来越重要,尤其在.Net Core中使用指针也更多,在.Net Core是Unsafe类,有更多指针的操作。

unsafe并不是不安全的,只是通过unsafe标记后,该函数或者该代码块区域,内存管理由我们开发人员自己掌控(内存资源的分配和释放),在C#中,GC不管理该区域的内存.

先看看使用指针的代码

不安全的

{

byte* pIn=(byte*)srcData.Scan0.ToPointer();

byte* pOut=(byte*)dstData.Scan0.ToPointer();

int stride=srcData.Stride;

字节* p;

省略

三十

}

Scan0是个句柄(Intptr),通过ToPointer返回一个void 指针,再转换为byte指针.

来看看ToPointer函数内部

[安全关键]

私人不安全的虚空*m_value;

[安全安全关键]

[CLSCompliant(false)]

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]

[不可转换]

public unsafe void* ToPointer() //调用ToPointer返回void类型的指针

{

返回m_value;

}阅读Ptr源码

看看完整IntPtr句柄源码,去除一些游戏账号购买特性标签

[可序列化]

公共结构 IntPtr : ISerializedable

{

[System.Security.SecurityCritical]

不安全的私人虚空m_value;编译器将 void 处理得最接近 uint,因此需要显式强制转换来保留 int 行为

公共静态只读 IntPtr Zero;

将IntPtr与(IntPtr)0进行比较的快速方法,而IntPtr.Zero由于静态访问缓慢而无法正常工作

内部不安全的布尔是努尔()

{

返回 (this.m_value==null);

}

公共不安全的IntPtr(int值)

{

if WIN32

m_value=(void*)值;

else

m_value=(void*)(long)值;

endif

}

公共不安全的IntPtr(长数值)

{

if WIN32

m_value=(void*)checked((int)value);

else

m_value=(void*)值;

endif

}

公共不安全的IntPtr(void*值)

{

m_value=值;

}

私有不安全的IntPtr(SerializationInfo info, StreamingContext context)

{

长 l=信息。GetInt64("value");

if (Size==4 && (l > Int32.MaxValue || l < Int32.MinValue))

{

throw new ArgumentException(Environment.GetResourceString("Serialization_InvalidPtrValue"));

}

m_value=(void*)l;

}

公共不安全覆盖布尔等于(对象 obj)

{

if (obj is IntPtr)

{

return (m_value==((IntPtr)obj).m_value);

}

返回 false;

}

public unsafe override int GetHashCode()

{

返回未选中((int)((long)m_value));

}

公共不安全 int ToInt32()

{

if WIN32

返回 (整数)m_value;

else

长 l=(长)m_value;

返回 checked((int)l);

endif

}

公共不安全长 ToInt64()

{

if WIN32

返回 (长)(英特)m_value;

else

返回(多头)m_value;

endif

}

公共不安全覆盖字符串 ToString()

{

if WIN32

返回 ((int)m_value)。ToString(CultureInfo.InvariantCulture);

else

返回((长)m_value)。ToString(CultureInfo.InvariantCulture);

endif

}

公共不安全字符串 ToString(字符串格式)

{

if WIN32

返回 ((int)m_value)。ToString(format, CultureInfo.InvariantCulture);

else

返回((长)m_value)。ToString(format, CultureInfo.InvariantCulture);

endif

}

公共静态显式运算符 IntPtr(int 值)

{

return new IntPtr(value);

}

public static explicit operator IntPtr(long value)

{

return new IntPtr(value);

}

public static unsafe explicit operator IntPtr(void* value)

{

return new IntPtr(value);

}

public static unsafe explicit operator void*(IntPtr value)

{

return value.m_value;

}

public unsafe static explicit operator int(IntPtr value)

{

if WIN32

return (int)value.m_value;

else

long l=(long)value.m_value;

return checked((int)l);

endif

}

public unsafe static explicit operator long(IntPtr value)

{

if WIN32

return (long)(int)value.m_value;

else

return (long)value.m_value;

endif

}

public unsafe static bool operator==(IntPtr value1, IntPtr value2)

{

返回值1.m_值==值2.m_值;

}

公共不安全静态布尔运算符 !=(IntPtr 值 1,IntPtr 值 2)

{

返回值1.m_值 !=value2.m_value;

}

public static IntPtr Add(IntPtr 指针,int offset)

{

返回指针 + 偏移量;

}

公共静态 IntPtr 运算符 +(IntPtr 指针,int offset)

{

if WIN32

返回新的 IntPtr(指针。ToInt32() + 偏移量);

else

返回新的 IntPtr(指针。ToInt64() + 偏移量);

endif

}

公共静态 IntPtr 减去(IntPtr 指针,int offset)

{

返回指针 - 偏移量;

}

公共静态 IntPtr 运算符 -(IntPtr 指针,int offset)

{

if WIN32

返回新的 IntPtr(指针。ToInt32() - 偏移量);

else

返回新的 IntPtr(指针。ToInt64() - 偏移量);

endif

}

公共静态整数大小

{

获取

{

if WIN32

返回 4;

else

返回 8;

endif

}

}

公共不安全的 void* ToPointer()

{

返回m_value;

}

}

智齿客服