Overloading getter and setter causes a stack overflow in C# [duplicate]

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;
}

Leave a Comment