相关疑难解决方法(0)

何时使用包含引用类型的值类型的数组而不是引用类型的数组?

假设我有以下内容:

public class MyElement
{
}

[Serializable]
[StructLayout(LayoutKind.Sequential)]
struct ArrayElement
{
    internal MyElement Element;
}

public class MyClass
{
    internal MyElement ComputeElement(int index)
    {
        // This method does a lengthy computation.
        // The actual return value is not so simple.
        return new MyElement();
    }

    internal MyElement GetChild(ref MyElement element, int index)
    {
        if (element != null)
        {
            return element;
        }

        var elem = ComputeElement(index);
        if (Interlocked.CompareExchange(ref element, elem, null) != null)
        {
            elem = element;
        }

        return elem;
    }
} …
Run Code Online (Sandbox Code Playgroud)

c# arrays caching value-type

6
推荐指数
1
解决办法
269
查看次数

标签 统计

arrays ×1

c# ×1

caching ×1

value-type ×1