Out parameter in C#

By
We already know that if we specify the return type of any method then we can get only one value at a time.But if we want method return more than one value at a time then we must use out parameter in the function. We need to use out keyword while declaring the parameter as well as while calling the method.
I have explained this concept with the help of suitable program which is give below.Please follow following steps.

Step 1-First open your visual studio->File->New->project->console Application->click OK->and write the following program which is given below:
see it-

using System;
namespace outparameter
{
    class Program
    {
        public class student
        {
            public void show(int a, int b, out int sum, out int multi)
            {
                sum = a + b;
                multi = a * b;
            }
        }
        static void Main(string[] args)
        {
            int val1,val2,sum1,mult1 ;
            Console.WriteLine("enter the  two  integer values");
 //here i have converted string value to integer you can use Int32.Parse or Convert.ToInt32 but there are some differences.
            val1 = Int32.Parse(Console.ReadLine());
            val2 = Convert.ToInt32(Console.ReadLine()); 
            student obj = new student();
            obj.show(val1, val2, out sum1, out mult1);
            Console.WriteLine("sum of value is" + " " + sum1);
            Console.WriteLine("multi of value is" + " " + mult1);
            Console.ReadLine();

        }
    }
}
Step 2- Now Run the program(press F5).
output:

Description:-  
  • In this program I have created a student class .Within the student class ,i have created a show method which has 4 parameter.first two parameter is input parameter and next two parameter is output parameter.EX. public void show(int a, int b, out int sum, out int multi).
  • After that i have created the object of student class in main method for calling the show method of student class.Ex. obj.show(val1, val2, out sum1, out mult1);
  • Show function calculate the value of sum and multi by passing the input parameter from main method.After that sum and multi are copied to the sum1 and mult1 respectively. Ex. sum1 <--sum; mult1 <--multi
  • After that sum1 and mult1 printed.
Note:- Here show function takes two values at a time by using out parameter.
For More:-

  1. Param Keyword
  2. Operator overloading
  3. Reflection
  4.  File Handling Real Application
  5.  Create a Setup File

I hope this is helpful for you.
To Get the Latest  Free Updates Subscribe
Click below for download whole application
    Download

2 comments:

  1. Thank you man, you really helping me on these tutorials. I just wish you can have a big project that combines all of them. I would like to see how they would fair...only if it is possible, sir.

    Props to you though, your tutorials are really interesting and fully explained.. Once again, props to you.

    ReplyDelete

Powered by Blogger.