次のものがあるとします。
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;
}
}
public class MyClassA : MyClass
{
readonly MyElement[] children = new MyElement[10];
public MyElement GetChild(int index)
{
return GetChild(ref children[index], index);
}
}
public class MyClassB : MyClass
{
readonly ArrayElement[] children = new ArrayElement[10];
public MyElement GetChild(int index)
{
return GetChild(ref children[index].Element, index);
}
}
MyClassB
overを使用するとどのような状況で利点がありますMyClassA
か?