Consider the following code:
请考虑以下代码:
public class LimitedBuffer<T> {
/// <summary>
///
/// </summary>
public const int DefaultSize = 10;
/// <summary>
///
/// </summary>
private T[] buffer;
/// <summary>
///
/// </summary>
private int poe;
/// <summary>
///
/// </summary>
private int pow;
/// <summary>
///
/// </summary>
/// <param name="size"></param>
public LimitedBuffer(int size = 10) {
// Buffer instantiation
this.buffer = new T[10];
// Value initialization
for (int i = 0; i < this.buffer.Length; i++)
this.buffer[i] = default(T);
this.pow = 0;
this.poe = size - 1;
}
/// <summary>
///
/// </summary>
private void ShiftCursors() {
if (this.pow == this.buffer.Length - 1) {
this.pow = 0;
this.poe = this.buffer.Length - 1;
} else
this.poe = this.pow++;
}
/// <summary>
///
/// </summary>
public T Value {
get {
return this.buffer[this.poe];
}
set {
this.buffer[this.pow] = value;
this.ShiftCursors();
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString() {
string ret = "LimitedBuffer = { ";
foreach (T el in this.buffer) {
ret += this.ToString() + "; ";
}
return ret + "}";
}
} /* LimitedQueue */
class Program {
static void Main(string[] args) {
LimitedBuffer<float> lb = new LimitedBuffer<float>(4);
lb.Value = 1.0f;
lb.ToString();
lb.Value = 2.0f;
lb.ToString();
lb.Value = 3.0f;
lb.ToString();
lb.Value = 4.0f;
lb.ToString();
lb.Value = 5.0f;
lb.ToString();
lb.Value = 6.0f;
lb.ToString();
lb.Value = 7.0f;
lb.ToString();
lb.Value = 8.0f;
lb.ToString();
lb.Value = 9.0f;
lb.ToString();
System.Threading.Thread.Sleep(20000);
}
Well, in the point I outlined using "HERE" the program dies.
好吧,在我用“HERE”概述的那一点上,程序就死了。
I am sure because, running the debugger, when it arrives to that line, and processes it, after processing that line everything crashes... I REALLY DON'T KNOW WHY!
我确定因为,运行调试器,当它到达该行并处理它时,在处理该行之后一切都崩溃了......我真的不知道为什么!
Can you help me please?
你能帮我吗?
Thankyou
谢谢
2 个解决方案
#1
7
The error is in your ToString
method. You are calling this.ToString
within it which causes infinite recursion and a *Exception
错误在您的ToString方法中。你正在调用this.ToString,它会导致无限递归和*Exception
public override string ToString()
{
string ret = "LimitedBuffer = { ";
foreach (T el in this.buffer)
{
ret += this.ToString() + "; ";
}
return ret + "}";
}
You probably want:
你可能想要:
public override string ToString()
{
string ret = "LimitedBuffer = { ";
foreach (T el in this.buffer)
{
ret += el.ToString() + "; ";
}
return ret + "}";
}
#2
1
The problem is that you're calling ToString
recursively:
问题是你以递归方式调用ToString:
public override string ToString()
{
string ret = "LimitedBuffer = { ";
foreach (T el in this.buffer)
{
ret += this.ToString() + "; ";
}
return ret + "}";
}
#1
7
The error is in your ToString
method. You are calling this.ToString
within it which causes infinite recursion and a *Exception
错误在您的ToString方法中。你正在调用this.ToString,它会导致无限递归和*Exception
public override string ToString()
{
string ret = "LimitedBuffer = { ";
foreach (T el in this.buffer)
{
ret += this.ToString() + "; ";
}
return ret + "}";
}
You probably want:
你可能想要:
public override string ToString()
{
string ret = "LimitedBuffer = { ";
foreach (T el in this.buffer)
{
ret += el.ToString() + "; ";
}
return ret + "}";
}
#2
1
The problem is that you're calling ToString
recursively:
问题是你以递归方式调用ToString:
public override string ToString()
{
string ret = "LimitedBuffer = { ";
foreach (T el in this.buffer)
{
ret += this.ToString() + "; ";
}
return ret + "}";
}