Beginner in C# need some advice

GrantPsych

New member
Joined
May 11, 2019
Messages
3
Programming Experience
Beginner
Hello,
I am currently trying to learn C# by following this website https://csharp.net-tutorials.com/basics/function-parameters/

A
t the moment, I am stuck at that page trying to figure out the 'out' parameters.
This is the code example:
-----------------------------------------------------
static void Main(string[] args)
{
int number = 20;
AddFive(ref number);
Console.WriteLine(number);
Console.ReadKey();
}

static void AddFive(ref int number)
{
number = number + 5;
}
---------------------------------------------------------
This is the excerpt from the tutorial:
'Using the out modifier is just like using the ref modifier, as shown above. Simply change the ref keyword to the out keyword. In the example above, also remember to remove the value assigned to number in the method and declare it in the call function instead.'

I am having difficulty understanding what the exercept mean by 'remove the value assigned to number and declare it in the call function.
I switched the 'ref' to 'out' but instantly felt lost as to the next step. Thank you in advance!
 
Parameters are usually for passing data into a method. A parameter declared ref is for passing data into and out of a method while a parameter declared out is for passing data out of a method only. In your current code, you are assigning a value to a variable outside the method, passing that value in, changing it and then passing it back out again. That makes sense for a ref parameter but not for an out parameter. In that case, you would not assign anything to the number variable before calling the method and you would not use the current value of the parameter inside the method, e.g. change your method to this:
C#:
static void AddFive(out int number)
{
    number = 5;
}
and then call it like this:
C#:
int number;
AddFive(out number);
 
Parameters are usually for passing data into a method. A parameter declared ref is for passing data into and out of a method while a parameter declared out is for passing data out of a method only. In your current code, you are assigning a value to a variable outside the method, passing that value in, changing it and then passing it back out again. That makes sense for a ref parameter but not for an out parameter. In that case, you would not assign anything to the number variable before calling the method and you would not use the current value of the parameter inside the method, e.g. change your method to this:
C#:
static void AddFive(out int number)
{
    number = 5;
}
and then call it like this:
C#:
int number;
AddFive(out number);

Thank you for your reply. Therefore, correct me if I am wrong, based on your explanation the 'out' and 'ref' parameters differ in where you would assign the value?
 
Thank you for your reply. Therefore, correct me if I am wrong, based on your explanation the 'out' and 'ref' parameters differ in where you would assign the value?
Yes, but that's not the whole story. They have different purposes and it's important to understand those purposes. If you do then everything else follows naturally from that.

One problem is that examples used to demonstrate principles to beginners are often rather contrived and thus do not really show why you would do something, only how you would do it. For instance, in your original example, you generally wouldn't use a ref parameter in that scenario. A void method with a single ref parameter doesn't really make sense. You should always return a value form a function as a first choice and only use out or ref parameters if you need to output multiple values. Here's the "rules" that I work by:

1. If a method has no outputs, its return type should be void.
2. If a method has one output then it should return that output and its return type should reflect that.
3. If a method has multiple outputs and one is logically the primary output then it should return that primary output and out or ref parameters should be used for the others.
4. If a method has multiple outputs and there is no logical primary output, the return type should be void and out or ref parameters should be used for all outputs.

In this particular case, the problem is that you are working backwards. You are being told that you need to use an out or ref parameter and you're trying to contrive a scenario in which you can do that, but that's not how it works when you're actually building an application. Each time you need to write a method to do something and you're specifying parameters, you just need to ask yourself what the purpose of the parameter is and that will tell you whether you need to declare it out or ref or neither. Is the parameter being used to pass data into the method, out of the method or both? That is the only question you need to ask and that tells you how to declare the parameter and how to use it.

If the parameter is used to pass data into the method then you don't need out or ref. It should also be fairly obvious that you need to set the value of the parameter outside the method or else what are you passing in? You can still modify the parameter inside the method if that is useful but those changes will not have any effect outside the method.

If the parameter is used to pass data out of the method only then you need to use out. You can still set the variable passed as an argument before calling the method but what would be the point, given that that value cannot be used inside the method?

If the parameter is used to pass data into and out of the method then you need to use ref. Note that both passing data in and passing data out are optional, so your method should allow for the parameter to be null coming in and the calling code should allow for the fact that the variable may not be changed by the method.
 
Yes, but that's not the whole story. They have different purposes and it's important to understand those purposes. If you do then everything else follows naturally from that.

One problem is that examples used to demonstrate principles to beginners are often rather contrived and thus do not really show why you would do something, only how you would do it. For instance, in your original example, you generally wouldn't use a ref parameter in that scenario. A void method with a single ref parameter doesn't really make sense. You should always return a value form a function as a first choice and only use out or ref parameters if you need to output multiple values. Here's the "rules" that I work by:

1. If a method has no outputs, its return type should be void.
2. If a method has one output then it should return that output and its return type should reflect that.
3. If a method has multiple outputs and one is logically the primary output then it should return that primary output and out or ref parameters should be used for the others.
4. If a method has multiple outputs and there is no logical primary output, the return type should be void and out or ref parameters should be used for all outputs.

In this particular case, the problem is that you are working backwards. You are being told that you need to use an out or ref parameter and you're trying to contrive a scenario in which you can do that, but that's not how it works when you're actually building an application. Each time you need to write a method to do something and you're specifying parameters, you just need to ask yourself what the purpose of the parameter is and that will tell you whether you need to declare it out or ref or neither. Is the parameter being used to pass data into the method, out of the method or both? That is the only question you need to ask and that tells you how to declare the parameter and how to use it.

If the parameter is used to pass data into the method then you don't need out or ref. It should also be fairly obvious that you need to set the value of the parameter outside the method or else what are you passing in? You can still modify the parameter inside the method if that is useful but those changes will not have any effect outside the method.

If the parameter is used to pass data out of the method only then you need to use out. You can still set the variable passed as an argument before calling the method but what would be the point, given that that value cannot be used inside the method?

If the parameter is used to pass data into and out of the method then you need to use ref. Note that both passing data in and passing data out are optional, so your method should allow for the parameter to be null coming in and the calling code should allow for the fact that the variable may not be changed by the method.

Thank you so much for the explanation! It will take some time for me to fully digest what you had just said. Really appreciate your help.
 
Back
Top Bottom