1

I have a class that looks like this:

public class someClass
{
    public someclass()
    {
        someString = "ABC";
    }

    public string someString
    {
        get => someString;
        set => someString = value;
    }
}

I understand that get and set are used for accessing and writing values to private fields. In my case, I am running into a stack overflow when I invoke 'someString', and I also know why (because the 'set' gets invoked in an infinite loop).

Can I do something to retain this implementation and not run into a stack overflow?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
SomeName
  • 27
  • 1
  • 4
    Just remove the self reference to somestring and leave the default behavior of [auto-properties](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties#auto-implemented-properties) to provide a hidden backing field. – StuartLC Aug 19 '21 at 06:58
  • 2
    The question title shows a misconception, which might be the underlying cause of your misunderstanding: `someString`, as defined by you, is *not* a "public field". It's a property. That's something completely different. You can think of a property as syntactic sugar for two methods (one returning a value, and one taking a value of the same type), nothing more, nothing less. *Usually* properties are used to set actual *fields* (as Connor Stoop demonstrates in his answer), and there is short-hand notation for that (see StuartLC's comment), but this is by no means guaranteed. – Heinzi Aug 19 '21 at 07:02
  • 1
    `can I do something to retain this implementation and not run into a stack overflow?` **Why** do you want to do this? https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – mjwills Aug 19 '21 at 07:12
  • 1
    Removing the duplicate closing (https://stackoverflow.com/questions/367192/why-does-property-set-throw-stackoverflow-exception), since OP explicitly wrote *"I also know why"*. Feel free to comment and explain your reasoning if you disagree, and I might change my opinion. – Heinzi Aug 19 '21 at 07:53
  • `Can I do something to retain this implementation and not run into a stack overflow?` "How can I make this code act like this, while also not acting like this?" The answer to your question, by definition, must be no. – mjwills Aug 19 '21 at 08:34

1 Answers1

4

Your property is refencing itself.
So when you set or get the value it is going to try en get the value from itself and this starts the loop.
You need a private to set en get the value from.
It should be:

private string someString;
public string SomeString
{
    get => someString;
    set => someString = value;
}

See the difference in capitalization

For completes you could also use a autoproperty as show by @StuartLC in the comments:

public string SomeString{ get; set; }
Connor Stoop
  • 1,574
  • 1
  • 12
  • 26
  • Thank you for the answer. I understand that. I have also mentioned in my question that I know ideally public properties are used with getters and setters to assign and read values to and from a private field. But my question was that with the self-referencing implementation of the field 'someString' remaining intact, is it still possible to change something else to not run into an infinite loop with the get and set? Or will it always happen no matter what? – SomeName Aug 19 '21 at 07:41
  • @SomeName: No, it's not possible. If you write a method `void A() { A(); }`, you *will* run into a a StackOverflow exception (or an infinite loop with tail call elimination, which is even worse). As explained in my comment above, a setter is just a fancy name for a method. – Heinzi Aug 19 '21 at 07:52