89

Suppose I have:

public class Bob
{
    public int Value { get; set; }
}

I want to pass the Value member as an out parameter like

Int32.TryParse("123", out bob.Value);

but I get a compilation error, "'out' argument is not classified as a variable." Is there any way to achieve this, or am I going to have to extract a variable, à la:

int value;
Int32.TryParse("123", out value);
bob.Value = value;
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
thorncp
  • 3,587
  • 3
  • 24
  • 20
  • possible duplicate of [Is it possible to pass properties as "out" or "ref" parameters?](http://stackoverflow.com/questions/564557/is-it-possible-to-pass-properties-as-out-or-ref-parameters) – nawfal Feb 11 '13 at 10:41

2 Answers2

87

You'd have to explicitly use a field and "normal" property instead of an auto-implemented property:

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, that will only work within the same class, as the field is private (and should be!).

Properties just don't let you do this. Even in VB where you can pass a property by reference or use it as an out parameter, there's basically an extra temporary variable.

If you didn't care about the return value of TryParse, you could 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");

That way you can use a single temporary variable even if you need to do this in multiple places.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I think the problem with the first approach is that if you use the field directly (instead of the property), u might be bypassing validation rules that may exist in the property – Andreas Grech Sep 02 '09 at 21:39
  • @Dreas: if it's an autoimplemented property beforehand, it can't possibly have any validation rules... – Jon Skeet Sep 02 '09 at 21:42
  • 3
    @Jon:I think you would like to change int.Parse to int.TryParse in the ParseOrDefault example. – shahkalpesh Sep 02 '09 at 21:44
  • 9
    Btw, the reason why you can't have a property as an output parameter is that setting a property actually is a function call and no assignment if you look under the hood. – Dirk Vollmar Sep 02 '09 at 21:48
  • @Jon yes, but what I'm saying is that if validation is later on added to the property, it will be skipped when using the first approach. – Andreas Grech Sep 02 '09 at 22:01
  • @Dreas: Yes, in that case it would fail. The `ParseOrDefault` method would help in that case. – Jon Skeet Sep 02 '09 at 22:25
  • @Jon: the problem with the ParseOrDefault() function is that only way to know if function succeed is to compare the return value with default int type, which is 0. If you give "0" as parameter, it will be considered as invalid (same as if you give "foo"), while this value can be converted to a int. i think having a function like 'static int Parse(string text, out bool success)' would be better. – tigrou Nov 15 '12 at 13:49
  • @tigrou: That method already exists - `int.TryParse` - just swapping the return value and `out` parameter. This method is *specifically* for the case where you don't care, which is why I prefixed it with: "If you didn't care about the return value of TryParse" – Jon Skeet Nov 15 '12 at 14:49
  • 4
    I would love to see the proper syntactic sugar to let me pass a property as a ref or out parameter. Let's do the work to the compiler, it's faster than me. – Ignacio Soler Garcia Oct 22 '13 at 09:34
  • @SoMoS: What would that mean though, exactly? Would it mean "assign the value when the method returns"? Because that's not the same as the behaviour using `ref` for variables... – Jon Skeet Oct 22 '13 at 12:02
  • @JonSkeet: well, having in mind that it's a value type makes sense to do that IMHO. – Ignacio Soler Garcia Oct 22 '13 at 12:18
  • 1
    @SoMoS: Whether it's a value type or not makes no difference to my comment. Either a change within the method to the parameter immediately takes effect, or it doesn't. This affects what happens if an exception is thrown, too. – Jon Skeet Oct 22 '13 at 12:35
  • Just wanted to point out that one has to be mindful if implementing INotifyPropertyChanged though. – Tipx Jul 21 '17 at 15:46
8

You can achieve that, but not with a property.

public class Bob {
    public int Value { get; set; } // This is a property

    public int AnotherValue; // This is a field
}

You cannot use out on Value, but you can on AnotherValue.

This will work

Int32.TryParse("123", out bob.AnotherValue);

But, common guidelines tells you not to make a class field public. So you should use the temporary variable approach.

Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101