aqwert
aqwert

Reputation: 10789

Why do TryParse methods uses an out parameter and not a ref

Somewhat on the back of this question that asks about the behaviour of the out parameter but more focused as to why these TryParse methods use out and not ref.

There have been some scenarios where you do want to initialise a value to the argument before parsing and keep that when the parsing fails but do not really care if it does fail. However because of the out parameter the value is reset.

This scenario could look like this...

int arg = 123;
Int32.TryParse(someString, ref arg);

However because of the out parameter we have to write it like this, which is more verbose...

int arg;
if(!Int32.TryParse(someString, out arg)
{
    arg = 123;
}

I realise that knowing that the parsing failed can be very useful however the use of ref does not preclude this.

So why do these TryParse methods use out and not ref?

Upvotes: 13

Views: 5913

Answers (4)

John Feminella
John Feminella

Reputation: 311526

You use out to indicate that the parameter is not being used, only set. The caller is required to assign a value before the method returns:

int n;
if (Int32.TryParse("47", out n)) { // Works fine; `n` will be set to the
  // ..                            // result of the parse.
}

If you used ref, you would have to initialize the value beforehand, which is silly since it's going to be overwritten anyway:

int n;
if (Int32.TryParse("47", ref n)) { // Kablammo! `n` isn't initialized.
  // ..
}

That's precisely the point of TryParse: you're guaranteed to have a value in the out parameter that represents the output of the parsing attempt. The return value of true or false indicates whether you should care about the result or ignore it.

Upvotes: 5

Kendall Frey
Kendall Frey

Reputation: 44326

In my experience anyway, there are many more times when you do not want to bother with initialization, than when you want to provide a default value. Parse was designed with this in mind.

The question is a good one, and I don't think there is a serious problem using one approach or the other. The .NET team chose the out approach, since they couldn't use both without adding a separate method to int, which would be disgustingly redundant.

Upvotes: 0

Guffa
Guffa

Reputation: 700362

That's because an out parameter fits better to what the method does.

If it used a ref parameter, then you have to provide an input to the method. This would not work:

int arg;
if (Int32.TryParse(someString, ref arg)) { // error - variable is not initialsed

The method doesn't expect any input in the value parameter, if you would have to provide an input value, it's not obvious what that would be, or how that would be used.

Upvotes: 3

SLaks
SLaks

Reputation: 887449

Because the normal use pattern is exactly the opposite of what you're describing.

People should be able to write

int arg;
if (!Int32.TryParse(someString, ref arg)) {
    Waaah;
}

Had TryParse taken a ref parameter, this would require a useless initialization.

The real question is why there isn't an int? int.TryParse(string) method.

Upvotes: 15

Related Questions