我正在为我的兄弟构建一个应用程序,该应用程序需要字符串输入形式的外部来源,GPS绘图仪,我想要在多行文本框中显示。如果有人要让这个应用程序长时间运行,我不希望TextBox.MaxLength被超过或内存分配变得过度。C#字符串数组缓冲区
我想不出一个很好的实现方法。我需要在文本框中显示最后n行。
编辑:标记的解决方案是正确的。谢谢乔恩。我结束了它的通用。以下是我用过的。
public class ArrayBuffer<T>
{
private readonly int _maxLines;
private int _writePosition;
private readonly T[] _buffer;
public ArrayBuffer(int maxLines = 100)
{
_maxLines = maxLines;
_buffer = new T[_maxLines];
}
public T[] Push(T value)
{
_buffer[_writePosition++] = value;
_writePosition %= _maxLines;
return _buffer.Skip(_writePosition)
.Concat(_buffer.Take(_writePosition))
.Where(line => line != null).ToArray();
}
}
用法:
var myArrayBuffer = new ArrayBuffer<string>(50);
string[] bufferedStringArray = myArrayBuffer.Push("some string");
也许醒酒第一? –
哈哈。骑马[鲍尔默峰](http://xkcd.com/323/) – TheGwa