C# System.StackOverflowException ,当使用 get set Properties 时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9798697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.StackOverflowException , when get set Properties are used?
提问by Mourya
An unhandled exception of type 'System.StackOverflowException' occurred in wcfserviceLibrary.DLL
wcfserviceLibrary.DLL 中发生类型为“System.StackOverflowException”的未处理异常
the code is show as follows.
代码如下所示。
[DataContract]
public class memberdesignations
{
[DataMember]
public string DesigId
{
get { return DesigId; }
set { DesigId = value;}
}
[DataMember]
public string DesignationName
{
get { return DesignationName; }
set { DesignationName = value; }
}
}
then i have method of Type memberdesignations as follows
然后我有类型成员指定的方法如下
public List<memberdesignations> memberdesignations()
{
List<memberdesignations> designations = new List<memberdesignations>();
memberdesignations objmemDesignations;
ds = objbll.Get_Member_Designations();
DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
foreach (DataRowView drow in dv)
{
objmemDesignations = new memberdesignations();
objmemDesignations.DesigId = drow["DesignationId"].ToString();
objmemDesignations.DesignationName = drow["DesignationName"].ToString();
designations.Add(objmemDesignations);
}
return designations;
}
iam getting the error in the class containing the get set properties.
我在包含 get set 属性的类中收到错误。
But i was able to get rid of the error when i modified the class like this :
但是当我像这样修改类时,我能够摆脱错误:
[DataContract]
public class memberdesignations
{
[DataMember]
public string DesigId
{
get; set;
}
[DataMember]
public string DesignationName
{
get; set;
}
}
On searching the forum , i found the cause for it was and explained by Konamiman here
在搜索论坛时,我找到了原因,并由 Konamiman在这里解释
i would like to know the difference between the two different ways for properties explained by Konamiman
我想知道 Konamiman 解释的属性的两种不同方式之间的区别
or any other explanation would be appreciated.
或任何其他解释将不胜感激。
Thanks
谢谢
采纳答案by Killnine
The issue is that, as Konamiman said, you are calling a property recursively.
问题在于,正如 Konamiman 所说,您正在递归调用属性。
Let's say I have a string property "DesignationName".
假设我有一个字符串属性“DesignationName”。
public string DesignationName
{
//Some getter to return some data
//Some setter to set the data
}
What would you expect it to return? How about returning a hard-coded string _designationName;
你希望它返回什么?返回一个硬编码的字符串 _designationName 怎么样?
private string _designationName = "someName";
public string DesignationName
{
get {return _designationName;}
//Some setter to set the data
}
That works. But what would happen if I had it return itself,instead?
那个有效。但是如果我让它自己返回会发生什么?
public string DesignationName
{
get {return DesignatioName;}
//Some setter to set the data
}
Well, it would keep calling DesignationName, which would keep calling itself again, which would again call DesignationName...and so on. All of this puts data on the stack, and goes on forever until is overruns the allocated space for the stack. Voila, a stackoverflow.
嗯,它会继续调用 DesignationName,它会再次调用自己,它会再次调用 DesignationName...等等。所有这些都将数据放在堆栈上,并一直持续下去,直到超出为堆栈分配的空间。瞧,一个stackoverflow。
The reason your last example works is because it is using what is called an 'autoproperty', a new feature to .NET 3.0. Basically, behind the scenes, it is creating backing fields for your properties so that this:
您的上一个示例之所以有效,是因为它使用了所谓的“自动属性”,这是 .NET 3.0 的一项新功能。基本上,在幕后,它正在为您的属性创建支持字段,以便:
public string DesignationName
{
get;
set;
}
actually compiles to behave like this:
实际上编译的行为是这样的:
private string _designationName = string.Empty;
public string DesignationName
{
get { return _designationName; }
set { _designationName = value; }
}
回答by TGH
You are referring to the property itself in the setter, so it will be calling itself recursively.(Over and over again until your stack overflows)
您指的是 setter 中的属性本身,因此它将递归调用自身。(一遍又一遍,直到堆栈溢出)
By using the short hand notation with just get; and set;, you are basically adding an implied backing field (like a backing variable). This way your not triggering a recursive calls since your property is just a wrapper around the backing field.
通过在 get 中使用简写符号;并设置;,您基本上是在添加一个隐含的支持字段(如支持变量)。这样您就不会触发递归调用,因为您的属性只是支持字段的包装器。
回答by Tanveer-Ibn- Haresh
declare private variables for both : _desigId, _designationName. You are in a recursive-loop that will go on infinitely. return the private variables, rather than the properties.
为两者声明私有变量:_desigId、_designationName。您处于一个无限循环的递归循环中。返回私有变量,而不是属性。

