What is the difference between ref and out parameters?

_config.yml

Looks the same to me! Wait! They’re actually slightly different.

Assumed Knowledge:

• Assuming you know what a: method and parameter is.

• Assuming you know what “passing a parameter” means.

• Assuming you know the difference between passing by reference and passing by value.

If you don’t know what they mean you are learning how to run before learning how to walk. Start with some of my more basic articles.

What is the purpose of these parameters?

First of all you gotta know what a parameter is. A parameter is something that is “passed” into a method. Ok so what is a method? A method is basically a verb – it’s something that gets done. For example, cooking is a method. Cleaning is a method. Working is a method. Anything that does something is a method. Computers can do stuff too: adding, subtracting are all methods.

Let’s break it down with an example.

Let’s take the “bake” method. Here’s how it works: you put some cake mix into the oven. And an hour later, out comes a baked cake! Let’s break it down further.

In: CakeMix
Out: BakedCake
Method: Bake

_config.yml

In this case, the CakeMix which is “passed into” the Bake method is basically called the expected parameter. What is mix? What’s the point of having that there? Well supposing you wanted to refer to mix while it is in the bake method – how are you going to do that unless you give it a name? For example, you want to check that the mix isn’t burning. The only way you can do that is if you have a way of referring to it.

class Example
        {
            public BakedCake Bake(CakeMix mix)
            {
                Console.WriteLine("oven turned on, the cake is getting baked");
                BakedCake cheeseCake = new BakedCake(mix);

                if (mix.CheckIfBurning())
	            {
		            Console.WriteLine("Turn oven off");
	            }
                return cheeseCake;
            }
        }

Ok Finally – what is the difference between a ref and out parameter?

• Ref requires us to “initialize” “variables” before passing them into methods. Initialize means that we must set our variable to equal something. Remember algebra in your Year 7 class? A variable is x, or y, or z. So basically initializing a variable means setting x = 10, or y = 5 or z = “hello” or abc = whatever you desire. That process of making x equal something else is a called initializing. Anyways, when you use ref, you must initialise the pertinent variable before passing it into the method. • Out does not require initialization prior to passing in, but it does require it to be initialized within the method.

int y;
Bar(ref y); // Error: you should set v to equal something before “passing it into” the Bar method


int x;
Foo(out x); // this works but you MUST initialize it in the Foo method.

Public void Foo(out x){
	x = 10   // x must be initialised or you’ll get an error.
}

………and as far as understanding what ref actually does, well you’ll need to have a good grasp of passing by reference and passing by value.

Written on December 16, 2016