我需要一个固定的数据结构,为了性能考虑它应该在栈上管理,但是它的行为应该像一个数组。
我知道我可以创建这样一个东西:
我知道我可以创建这样一个东西:
using System;
namespace X
{
public sealed struct CustomArray<T>
{
private const Int32 n = 2;
private T _field_1;
private T _field_2;
// ...
private T _field_n;
public T this[Int32 idx]
{
get
{
switch(idx)
{
case (0): return _field_1;
case (1): return _field_2;
// ...
case (n): return _field_n;
default: throw new IndexOutOfRangeException();
}
}
set
{
switch(idx)
{
case (0): _field_1 = value; break;
case (1): _field_2 = value; break;
// ...
case (n): _field_n = value; break;
default: throw new IndexOutOfRangeException();
}
}
}
}
}
但是这种方法对于由约50个元素组成的结构来说并不方便。有没有一种更加方便和易于维护的方法来实现这个呢?
提前感谢。