You need to explicitly use the field and the βnormalβ property instead of the property that was automatically implemented:
public class Bob { private int value; public int Value { get { return value; } set { this.value = value; } } }
Then you can pass the field as an out parameter:
Int32.TryParse("123", out bob.value);
But, of course, this will work only in one class, since the field is private (and it should be!).
Properties simply do not allow this. Even in VB, where you can pass a property by reference or use it as an out parameter, basically there is an additional temporary variable.
If you do not need to return a TryParse value, you can always write your own helper method:
static int ParseOrDefault(string text) { int tmp; int.TryParse(text, out tmp); return tmp; }
Then use:
bob.Value = Int32Helper.ParseOrDefault("123");
This way you can use one temporary variable, even if you need to do it in several places.
Jon Skeet Sep 02 '09 at 21:34 2009-09-02 21:34
source share