C# Out parameters Vs REF parameters

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
out and ref are one of those most confused topics in c-sharp and in this video we will try to simplify the same now there are three important points to remember about out and ref and once you understand these three points you know you will never have a confusion about out and ref so let us start with a point number one out and Ref are she sharp keywords which help you to pass variables to functions and methods by reference again I repeat out and ref helping to pass variable data to functions and methods by reference now what happens is by default when you pass variable from you know from a function to other function it is passed by Val means what for example over here you can see I have a simple static void main function so this is a caller and he is calling a function called as sum function so this is a colleague right and he is passing this outside where to him and this Here I am whatever data is passed I am trying to manipulate here by adding 20 to it a 10 to it okay so if you see now if I go and do a f11 so let us go step by step so I'm going to go and do a f11 so there it goes so first thing the main is the caller sum function is the collie so in the cover I am initializing this value outside where 220 and I am passing it to the function so okay I am passing it to the sum function so I will do a f11 here so you can see it has passed the variable here so you can see that it has passed 20 and now I am trying to manipulate this data over here so whatever it is is 20 I am adding 10 to it and this becomes 30 and when I come out you can see that my outside where is still value 20 means what whatever changes has happened to the inside variable here right is not affected outside in other words over here when this data was passed it was passed by well a new complete new copy of data was created and passed right but now let us say you want to go and pass this by reference so that's what the point number two comes so point number two so we said point number one out and ref they help you to pass data by reference okay so now if I want to go and pass this variable by reference what I will do is I will make this as ref and I will also say this as ref so now if I do a f11 now watch this so there it goes the outside where value is 20 it is passed inside he manipulates the value and if you see now you're outside variable is also manipulated in other words when this outside where value was passed to this sum function he not only passed the data but he also passed the reference so in other words here you know when we were manipulating this inside where we were actually manipulating outside where okay so ref helps you to pass your variables by reference so in other words if you have a collar and a collie and if you want to pass data to him and if you want to get the manipulated data in the same variable you will use the ref keyword now what is out so let us see out here okay so I'm going to go and make this as out again out is a keyword so you will just say you're out and let us see what happens now the first thing is you can see that you get an error here you know saying that you need to initialize the inside where okay so let me do that and I will tell you what exactly is this and this is what differentiates between out and ref so now again let us do a f11 let us see what happens so you can see that I am doing a f11 and again I would like to mention here that when you are also using out it is going to pass by reference so in other words here also you can see that outside where is passed by reference why I do a f11 and here you can see the way the value is zero it is made 10 and this same 10 is seen outside in the outside where so in other words in out also heat passes by reference but in out you have it passes only one way it passes only from the colony to the collar in other words if you try to send any kind of value from here you can see that I sent the value 20 but it was not taken inside so in other words in out you need to initialize the variables inside the function so even if you are passing data from the outside function it would be discarded this is the biggest difference between out and ref in ref it is two-way in ref you know he passes the data from the caller to the callee and also he passes the address and the manipulated data is again passed back while in out he passes the reference only from the caller to the callee and only the manipulated data goes out to the to the caller right so so just summarizing you know what is the difference between the left and out first thing ref and out our keywords are shisha keywords they help you to pass by reference the variables second ref is two-way it sends the data from the caller to the callee and again back from the quali to the caller out is out again passes by reference but it is only one way it sends the manipulated data from the Khali to the caller but from caller to the callee any kind of data sent would be discarded so I hope that you I enjoyed this video in this video we were trying to understand what exactly is the importance of out and ref keyword in shisha thank you very much
Info
Channel: .NET Interview Preparation videos
Views: 246,333
Rating: undefined out of 5
Keywords: Out and Ref, C# Out, C# Ref, C# (Programming Language), c# out parameters, c# ref parameters, Reference, c# method parameters, types of method parameters c#, types of parameters c#, c# ref parameter tutorial, c# out parameter, c# params parameter, c# dynamic parameter, difference between our and ref, out and ref parameters in c#, our parameters, ref parameters, ref and our in c#, csharp parameters type, ref vs out in c#, understand out vs ref, csharp out and ref tutorial
Id: lYdcY5zulXA
Channel Id: undefined
Length: 6min 42sec (402 seconds)
Published: Mon Sep 28 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.