ARTICLE

Difference between Ref Keyword And Out Keyword

Posted by Mayur Gujrathi Articles | Visual C# April 21, 2011
While passing Argument To Any Method Having ref and out keyword in C#.
Reader Level:

While passing an argument to any method having a ref or out keyword, as we can see in the following program, that while calling method math1 we have to initialize a variable as "int b=0;" and in the same manner while calling math2 it is optional to initialize a variable for passing an argument. Let's see a small program.
 

 namespace
OOPSProject
 {
     class Program
 
    {
         //No Input & Output
 
        void Test1()
         {
             Console.WriteLine("First method");
         }
         //No Output has Input
 
        void Test2(int x)
         {
             Console.WriteLine("Second method: " + x);
         }
         //No Input has Output
 
        string Test3()
         {
             return "Third method";
         }
         //Has Input & Output
 
        string Test4(string name)
         {
             return "Hello " + name;
         }
         //Returning multiple values:
 
        int Math1(int x, int y, ref int z)
         {
             z = x * y;           
             return x + y;
         }
         int Math2(int x, int y, out int z)
         {
             z = x * y;
             return x + y;
         }
         static void Main(string[] args)
         {
             Program p = new Program();           
             p.Test1(); p.Test2(100);
             Console.WriteLine(p.Test3());
             Console.WriteLine(p.Test4("Raju"));
             int b = 0;  //Initialization is mandatory
 
            int a = p.Math1(100, 50, ref b);
             Console.WriteLine(a + "   " + b);
             int n;  //Initialization is optional
 
            int m = p.Math2(100, 50, out n);
             Console.WriteLine(m + "   " + n);
             Console.ReadLine();
 
      }
     }
 }

Login to add your contents and source code to this article
post comment
     

Please any one tell me the Banagar Raju sir blog address

Posted by BhanuPrasanna Kumar Mar 18, 2013

Well i am also mr.bangar raju student he is just amazing :) well guys u know any groups of c#.net in yahoo etc for bagar raju sir

Posted by SUNIL GUTTA Feb 05, 2013

sort and sweet

Posted by Ankit Nandekar Apr 21, 2011

Yes, he is the ultimate.......

Posted by Raju Katare Apr 21, 2011

Hey raju thanks for comments, I dont know how many of bangarraju students are here, I have met to lot of students here only. he is great. isnt he? well good to know you are also a bangarraju student. still other articles are coming from my side

Posted by Mayur Gujrathi Apr 21, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter