FreeOnTheBar.exe 中发生了 'System.StackOverflowException' 类型的未处理异常
An unhandled exception of type 'System.StackOverflowException' occurred in FreeOnTheBar.exe
在我的程序中抛出了一个WhosebugException
,我不明白为什么。
有人可以解释一下发生了什么吗?
此屏幕截图显示了抛出异常的确切位置:
Exception
我的代码:
class Program
{
static void Main(string[] args)
{
var t = new WhiteWine();
t.Name = "Tom";
t.year = 1414;
string f = t.Prepare;
}
}
class WhiteWine : Wine
{
public override string Prepare
{
get
{
return $"Well, {this.Name} is a cold wine, therefor it served cold. \n All you need to di is take it out from the refrigerator, pour it into a glass and serve.";
}
}
public override string Name
{
get
{
return $"{this.Name} ({this.year})";
}
}
}
public class Wine : Drink
{
public int year { get; set; }
}
public class Drink : Idrink
{
public virtual string Name { get; set; }
public virtual string Prepare { get; }
}
WhiteWine.Name
get 访问器会产生无限递归,因为您使用 this.Name
来计算 this.Name
的值。
public override string Name
{
get
{
return $"{base.Name} ({this.year})";
}
}
这应该可以解决问题。
在我的程序中抛出了一个WhosebugException
,我不明白为什么。
有人可以解释一下发生了什么吗?
此屏幕截图显示了抛出异常的确切位置: Exception
我的代码:
class Program
{
static void Main(string[] args)
{
var t = new WhiteWine();
t.Name = "Tom";
t.year = 1414;
string f = t.Prepare;
}
}
class WhiteWine : Wine
{
public override string Prepare
{
get
{
return $"Well, {this.Name} is a cold wine, therefor it served cold. \n All you need to di is take it out from the refrigerator, pour it into a glass and serve.";
}
}
public override string Name
{
get
{
return $"{this.Name} ({this.year})";
}
}
}
public class Wine : Drink
{
public int year { get; set; }
}
public class Drink : Idrink
{
public virtual string Name { get; set; }
public virtual string Prepare { get; }
}
WhiteWine.Name
get 访问器会产生无限递归,因为您使用 this.Name
来计算 this.Name
的值。
public override string Name
{
get
{
return $"{base.Name} ({this.year})";
}
}
这应该可以解决问题。