InterviewSolution
Saved Bookmarks
| 1. |
What is the difference between ref and out keywords? |
Answer»
Example 1: // C# program to illustrate the concept of out parameterusing System; class GFG { // Main method static public void Main() { // Lets declare a string string str3 = "Scaler"; // Pass variable str3 to the method using ref keyword initializeString(ref str3); // Display the value str3 now // Since initially we declared value "Scaler" in str3, // normally below statement must print "Hello Scaler" // But due to the use of ref keyword, // the value will be overwritten in the initializeString() Method // So now the value printed will be "Hello InterviewBit" instead Console.WriteLine("Hello {0}", str3); } // Method in which ref parameter is passed and a value is written into this variable public static void initializeString(ref string str1) { // Since ref keyword supports bi-directional data flow, // This check statement will be validated and output will be printed if (str1 == "Scaler") { Console.WriteLine("Hello Scaler"); } // Now lets try to change the value of ref parameter str1 // The value "InterviewBit" will be overwritten in reference variable str3 str1 = "InterviewBit"; } } The above program will generate the below output: Hello ScalerHello InterviewBit
Example 2: Let’s see if and how the data flows when out keyword is used: // C# program to illustrate the concept of out parameterusing System; class GFG { // Main method static public void Main() { // Declaring a string variable // Since we are going to use out parameter // We do not need to assign any value to it string str1; // Pass variable str1 to the method using out keyword // Normally it should pass the default value, but due to use of out keyword // An empty container will be passed instead, leading to error checkIfScaler(out str1); // Lets try again with another value string str2 = "Scaler"; // Pass variable str2 to the method using out keyword // Now you must think that the value "Scaler" will be passed // But due to use of out keyword, // again an empty container will be passed instead, leading to error checkIfScaler(out str2); } // Method in which out parameter is passed and checked // if the current string value is Scaler or not public static void checkIfScaler(out string str1) { // Check parameter value // Since we are using out parameter, this will lead to error // as no data is passed when into this method when out keyword is used // This is because out keyword onlu facilitates uni-direction data transfer if (str1 == "Scaler") { Console.WriteLine("Hello!!Scaler"); } } } The above program will generate the below error: prog.cs(39,13): error CS0269: Use of unassigned out parameter `str1'Example 3: Now let us try to see what happens when we initialize some value to an out parameter. // C# program to illustrate the concept of out parameterusing System; class GFG { // Main method static public void Main() { // Lets declare a string string str3 = "Scaler"; // Pass variable str3 to the method using out keyword // Now you must think that the value "Scaler" will be passed // But due to use of out keyword, // again an empty container will be passed instead initializeString(out str3); // Display the value str1 now // Since initially we declared value "Scaler" in str3, // normally below statement must print "Hello Scaler" // But due to the use of out keyword, // the value will be overwritten in the initializeString() Method // So now the value printed will be "Hello InterviewBit" instead Console.WriteLine("Hello {0}", str3); } // Method in which out parameter is passed and a value is written into this variable public static void initializeString(out string str1) { // Since out keyword supports uni-directional data flow, // the value "InterviewBit" will be written in reference variable str3 str1 = "InterviewBit"; // Check parameter value if (str1 == "InterviewBit") { Console.WriteLine("InterviewBit value assigned successfully"); } } } The above program will generate the below output: InterviewBit value assigned successfullyHello InterviewBit |
|