3

I -sometimes- use out parameters, the method sometimes is complicated, and contain loops and conditional blocks, In most cases the compiler gives me this error

"The out parameter 'xxx' must be assigned to before control leaves the current method"

So, I find my self always start the method with nullifying all out parameters, I need to know, is it a good practice?

* EDIT *

I mean assign the default value by the word "Nullify"!

Saw
  • 6,199
  • 11
  • 53
  • 104
  • 1
    Are you asking if using out parameters is OK practice, or if nullifying them at the beginning of the calling method is good practice? No to the former, but if you "have to" do it, then yes to the latter IMO ;-) Some detail here: http://stackoverflow.com/questions/281036/are-out-parameters-a-bad-thing-in-net – Sepster Dec 24 '12 at 10:50
  • Asking: if nullifying them at the beginning of the method is good practice :) – Saw Dec 24 '12 at 10:51
  • There is also another case where `out` is useful. It could give you some performance improvements. http://stackoverflow.com/questions/10541532/what-is-the-benefit-of-using-out-ref-versus-returning – Lukasz Madon Dec 24 '12 at 11:10

8 Answers8

7

I wouldn't, as leaving them uninitialised will prompt you to set appropriate values in all code branches (as a compiler error), rather than forgetting and leaving the null/etc. If, however, you have multiple code paths and all but one of them use the same default value (like a TryGet / TryParse method), then it might be pragmatic to assign the default at the start, so there is only one other assignment.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

Depends on the meaning you apply to the null parameters.

Normally null means undefined, unknown value, which could not be retrieved by some error. When you encounter null value in one of your parameters it is usualy not normal program flow.

Thus, it is better to define set of uniform default values whose meaning will be ready to be filled and nullify them if something unexpected occurs.

Of course all of this depends on the projects and present standarts of codding and naming

Nogard
  • 1,779
  • 2
  • 18
  • 21
1

Personally I would probably return a class rather than use Out paramaters. They are great in the "TryParse" situations, but I think they can be quite confusing if over used.

Ross Dargan
  • 5,876
  • 4
  • 40
  • 53
  • 3
    On top they make it hard to work. Heck, TryParse also could work returning a nullable. The fact that an out parameter can not be used before makes it kind of awkward to use in many scenarios. – TomTom Dec 24 '12 at 10:52
1

You definitely need to make sure that the out parameter is set in the flow of the method. That is required by the compiler and it's not negotiable :)

One way to do that is to set it at the beginning to a default value, and that would work. However it could mask any issues where the flow of the method does not set the out parameter and it should.

So, it's best to use your method in enough unit tests, so that you are sure that the code works either way.

SWeko
  • 30,434
  • 10
  • 71
  • 106
1

If you know for a fact that they will be populated with some meaningful data then I wouldn't bother nulling them but if they get assigned values in a conditional expression then nulling them at the start of the method is probably fine. Setting a default value however may be a better option. I also try to use out params as sparingly as possible, I find that generally when you need them you've made a design error somewhere along the way.

Chris McCabe
  • 1,010
  • 11
  • 20
1

If u r assigning value in some loop or in If condition then its always better to initialize the out parameters at the start otherwise its not necessary....but for good practice we should always assign the value at the beginning

Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
0

Let's look at this piece of code;

public void Get(out int Id_1, out int Id_2)
{
    if ( )
    {
        Id_1 = Value1;
        Id_2 = Value2;
    }
}

We are assigning Id_1 and Id_2 inside an if statement and compiler can't determine if it will be assigned a value at run time. We should assign some default value of this variables before the if statement. Looks to me it is a good practise.

public void Get(out int Id_1, out int Id_2)
{
    Id_1 = 0;
    Id_2 = 0;

    if ( )
    {
        Id_1 = Value1;
        Id_2 = Value2;
    }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Why not Considaring Tuple <T> class instead of hell lot of out parameters......

Saw
  • 6,199
  • 11
  • 53
  • 104
TalentTuner
  • 17,262
  • 5
  • 38
  • 63