相关疑难解决方法(0)

你为什么要使用String.Equals而不是==?

我最近被介绍到一个大型代码库,并注意到所有的字符串比较都使用String.Equals()而不是==

你觉得这是什么原因?

c# string equals

306
推荐指数
8
解决办法
27万
查看次数

C#中对象的内存地址

我有一段时间写的功能(对于.NET 3.5),现在我已升级到4.0

我无法让它发挥作用.

功能是:

public static class MemoryAddress
{
    public static string Get(object a)
    {
        GCHandle handle = GCHandle.Alloc(a, GCHandleType.Pinned);
        IntPtr pointer = GCHandle.ToIntPtr(handle);
        handle.Free();
        return "0x" + pointer.ToString("X");
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我调用它时 - MemoryAddress.Get(新车("蓝色"))

public class Car
{
    public string Color;
    public Car(string color)
    {
        Color = color;
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

对象包含非原始或非blittable数据.

为什么它不再起作用了?

我现在如何获取托管对象的内存地址?

.net c#

66
推荐指数
6
解决办法
8万
查看次数

标签 统计

c# ×2

.net ×1

equals ×1

string ×1