StackOverflowException未处理

我的代码中出现此错误

MedCareProviderLibrary.dll中发生未处理的“System.StackOverflowException”类型exception

这是我的代码片段以及错误的来源。 它会在出现错误的部分给出一个黄色箭头。

显示错误的部分以粗体显示。 任何帮助将不胜感激谢谢

private string _TestNo; private string _TestType; private DateTime _TestDate; private string _PatientNo; private string _DoctorNo; public Test() { _TestNo = ""; _TestType = ""; _TestDate = new DateTime(); _PatientNo = ""; _DoctorNo = ""; } public Test(string aTestNo, string aTestType, DateTime aTestDate, string aPatientNo, string aDoctorNo) { _TestNo = aTestNo; _TestType = aTestType; _PatientNo = aPatientNo; _DoctorNo = aDoctorNo; } public string TestNo { set { _TestNo = value; } get { return (TestNo); } } public string TestType { set { _TestType = value; } **get { return (TestType); } } public DateTime TestDate { set { _TestDate = value; } get { return (TestDate); } } public string PatientNo { set { _PatientNo = value; } get { return (PatientNo); } } public string DoctorNo { set { _DoctorNo= value; } get { return (DoctorNo); } } 

您的所有属性getter都返回属性本身而不是下划线前缀字段名称。

 public string TestType { set { _TestType = value; } get { return (TestType); } } 

而不是return _TestType ,而是return TestType ,因此属性getter一次又一次地访问自身,导致无限递归并最终导致调用堆栈溢出。

此外,返回值不一定需要括号(除非您正在评估一些复杂的表达式,在这种情况下,您不是这样)。

更改您的getter以返回以下划线为前缀的字段(对所有属性执行此操作):

 public string TestType { set { _TestType = value; } get { return _TestType; } } 

或者如果你使用的是C#3.0,就像其他人建议的那样使它们成为自动属性 。

在你的属性中,你正在以递归方式调用get

  get {return TestNo; } 

这无法终止并继续调用自己,直到堆栈被StackOverflowException并抛出StackOverflowException

它应该是:

  get {return _TestNo; } 

您可以在C#3.0及更高版本上使用自动属性 ,并完全避免此问题:

 public string TestNo { get; set;} 

这当然适用于您拥有的所有其他属性

您必须在属性实现中返回支持字段而不是属性本身,否则该属性将递归调用自身并导致堆栈溢出:

 public string TestNo { set { _TestNo = value; } get {return _TestNo; } }//End of TestNo Properties 

由于您没有使用任何需要您自己实现属性的其他逻辑,我建议您使用自动属性:

 public string TestNo {get;set;} 

您递归调用get函数,不要引用要返回的对象。 它应该如下所示:

 public string TestNo { set { _TestNo = value; } get {return _TestNo; } }//End of TestNo Properties public string TestType { set { _TestType = value; } **get { return _TestType; }** }//End of TestType Properties public DateTime TestDate { set { _TestDate = value; } get { return _TestDate; } }//End of TestDate Properties public string PatientNo { set { _PatientNo = value; } get { return _PatientNo; } }//End of PatientNo Properties public string DoctorNo { set { _DoctorNo= value; } get { return _DoctorNo; } }//End of DoctorNo Properties 

您将返回属性而不是成员变量,从而导致递归。

例如:

 public string TestType { set { _TestType = value; } get { return (TestType); } } 

应该:

 public string TestType { set { _TestType = value; } get { return _TestType ; } } 

因为你试图返回属性本身(这会对get方法进行隐式调用),而后者又会一次又一次地尝试返回等等……所以你得到一个堆栈溢出。

你的代码应该是这样的:

 public DateTime TestDate { set { _TestDate = value; } get { return _TestDate; } }//End of TestDate Properties 

或者你可以使用自动属性:

 public DateTime TestDate { set; get; }//End of TestDate Properties 

您的属性自行返回,而不是您的成员变量,导致堆栈爆炸。 你可能想写的是:

 public string TestType { set { _TestType = value; } get { return _TestType; } }//End of TestType Properties