AutoProperty as out value

976 views Asked by At

Why exactly I can't use an AutoProperty as an out parameter?

For example (This gives me an error):

public int HeightValue { get; set; }

//...

private void Parse()
{
    int.TryParse(WidthText.Text, out HeightValue);
    //Intellisense Error: out argument is not classified as a variable
}
2

There are 2 answers

0
Piyush Parashar On BEST ANSWER

Possibly because properties are in essence methods and you need to give a field to set the value to the out parameter. You can define a backing field for your property and give its value as the out parameter.

See Jon Skeet's answer here:

Passing a property as an 'out' parameter in C#

0
AudioBubble On

The method itself needs a variable as the out parameter. It's got to have a storage location it can just write values to. Not a property, not anything it needs to invoke: just a storage location. A property doesn't satisfy that requirement. So there's nothing that can be done by the compiler in the method to allow this.