Today I facing the exact situation written in reference #1, I tried to pass the property to TryParse out parameter.
Below is the sample code:
public int I
int.TryParse(str, out I); // pass I (not i) as out parameter
But I got this error message:
A property or indexer may not be passed as an out or ref parameter.
Till now I cannot get any satisfactory explanation about this error, however I think the most acceptable answer for now is from reference #2.
The fact that a property looks and feels like a variable is deceiving. When compiled, a property becomes two methods: one for the Getter and one of the Setter. This is why the property fails when you try to use it in TryParse().
Solution is simple, just place the original variable as out parameter.
This question make me need to understand MSIL one day to find out what happen inside there…
- Mano’s Tech Blog – Different ways to convert string to an integer in C#
- Why TryParse Can’t Get Property?
Keyword: Accessor, get,set,getter,setter,error