Overloading Getter and Setter Causes a Stack Overflow in C#

Overloading getter and setter causes a stack overflow in C#

Yes, you do not have a backing field... this is how you should do it:

private MyEnumType data;

public MyEnumType Data
{
get
{
return data;
}
set
{
data = value;
}
}

What happens is that you are referring to the property to return itself, which causes an infinite loop of trying to access its own value. Hence, a stack overflow.

In your case when you do not add any additional logic in the get and set methods you could use an automatic property as well. This is simply defined like so:

public MyEnumType Data
{
get;
set;
}

Accessing list via getter causes stack overflow

return TextElements calls the getter for TextElements. Return the backing field instead.

private List<Text> textElements = new List<Text>();
public List<Text> TextElements {
get {
return textElements;
}
}

You can prevent this kind of mistake from happening by prefixing your private fields with an underscore. Then you know at a glance: If your getter gets (or your setter sets) something without an underscore prefix, you need to fix it. Intellisense is case blind, but it won't ever mistake _ for T.

private List<Text> _textElements = new List<Text>();
public List<Text> TextElements {
get {
return _textElements;
}
}

Stack overflow exception in C# setter

When you write a = value, you are calling the property setter again.

In order to use non-automatic properties, you need to create a separate private backing field, like this:

ConstraintSet a;
public ConstraintSet A { get { return a; } set { a = value; } }

Infinite loop in getter/setter c#

You have an infinite recursion, as you are referring to the property in the property.

You should use a backing field for this:

private int testCount;
public int TestCount
{
get { return testCount; }
set { testCount = value + 13; }
}

Note the property name TestCount (which also conforms to C# naming standard), as opposed to the field name testCount (lowercase t).

Request model throws stack overflow - .NET

Your property getter are calling their self. You need to store the value in a backing field.

Also public properties follow PascalCase convention

I don't know what are you trying in the Symbol setter, perhaps it sould be (Note that you will need to add exception management in case the symbol value doesn't exists in the symbols list):

try {
Market = this.symbols.Find(findSymbol => findSymbol.Key == value).Value;
_symbol = value;
} catch {
//Do exception handling
}

Here the complete code:

public class NewOrderModel
{
private readonly List<KeyValuePair<string, string>> symbols = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string> ("WINZ19", "XBMF"),
new KeyValuePair<string, string> ("WDOZ19", "XBMF")
};

private string _symbol;

[Required]
public string Symbol
{
get => _symbol;

set
{
Market = this.symbols.Find(findSymbol => findSymbol.Key == value).Value;
_symbol = value;
}
}

private string _market;
public string Market
{
get => _market;

private set
{
_market = value;
}
}

StackOverflowException: The requested operation caused a stack overflow

The problem is here:

public float actExp { get { return actExp; } set { actExp = 0f; } }
// ^ Problem here & ^ same problem here

You are calling your Property inside its own getter and setter. It creates an infinite loop... (everytime you call actExp it will call itself)

Instead you should just write (if there is no logic inside accessors):

public float actExp { get; set; }

Stack overflow error in C# set/get

The getter of the first example is returning the property itself, not a backing field.

// The property name is "FeedbackComment"
public string FeedbackComment
{
// And here you are returning "FeedbackComment" which is
// creating the stack overflow
get { return FeedbackComment; }
}

Unfortunately there is no way to shorten what you have, automatically implemented properties (i.e. public String FeedbackComment { get; set; }) must have empty getter and setter blocks to be syntactically correct. There is nothing wrong with your second example - yes, it is a bit verbose but it is clear, concise, and gets the job done.



Related Topics



Leave a reply



Submit