-2

I always see properties using a private variable to hold the value like this.

private int _myValue;
public int MyValue { get { return _myValue; } set { _myValue = value; } }

Why can't I just skip the private variable and do this...

public int MyValue { get { return MyValue; } set { MyValue = value; } }

*Note: I didn't want to use auto properties because I was hoping to be able to do something like this.

public int MyValue { get { return MyValue.Tolower().Trim(); } set { MyValue = value; } }
12
  • 8
    Run your second version. When it explodes, you'll understand why. Dec 13, 2011 at 16:39
  • 1
    Just out of curiosity, what kind business case would require you to not adhere to the syntax of working C# properties? Dec 13, 2011 at 16:40
  • 9
    Calling ToLower() on an int is the first problem you'll need to overcome ;) Dec 13, 2011 at 16:41
  • 2
    @SheldonWarkentin, I call it the legacy application business case and our managers simply don't understand how .NET works and don't want to take responsibility of recompiling the code in C# 3.0 business case. It's a pretty common business case seen in many enterprises. Dec 13, 2011 at 16:41
  • 2
    Sounds like you are more asking why the c# compiler can't apply some syntactic sugar/cleverness to allow a variable with the name of a Property within the scope of the property declaration to point to an autogenerated backing field? I'd actually be interested in a reason why not myself (as in a reason why it is technically impossible rather than simply the answer "that is not how the language works" which is true but not as interesting)
    – David Hall
    Dec 13, 2011 at 16:44

6 Answers 6

15

Because the infinite loop would eventually cause a StackOverflowException to be thrown.

1
5

Because without a variable you'd end up with an endless recursion, causing a stack overflow :-)

public int MyValue
{
    get { return MyValue; }
    set { MyValue = value; }
}  

Thus you need a backing field. The reason to keep the backing field private is to hide the implementation details. If it were public it wouldn't make any sense to declare a property to access it.

1
  • 2
    +1 for the great explanations in the link for endless recursion. Dec 13, 2011 at 16:43
2

Why can't I just skip the private variable and do this...

   public int MyValue { get { return MyValue; } set { MyValue = value; } }

Because return MyValue would recursively call the get accessor of the MyValue property, resulting in infinite recursion, and eventually a StackOverflowException (and similarly, MyValue = value would do the same with the set accessor)

0

The getter and setters are there with a backing property in case you want to do some kind of validation. If not then use auto properties, before auto properties were implemented this question came up and that's why they were implemented.

1
  • Maybe he is thinking about autoproperties I wonder if that's what he's wanting to use..
    – MethodMan
    Dec 13, 2011 at 16:40
0

As said earlier this would cause a stack overflow, the reason for that is that when you type MyValue = value you will be calling the set again in a infinit loop until the program runs out of stack space, and the same happens for the get.

public int MyValue { get { return MyValue; } set { MyValue = value; } }
0

In additional to Ians response, they allow you to initialze the property to a default value of your choosing, rather than null:

private string _myString = string.Empty;

public string MyString
{
     get{ return _myString;}
     set{ _myString = value;}
}

will return string.Empty rather than null if the setter has never been called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.