methods in c#

1> value parameter 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

non static methond > 
namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int j = 0;
            Program p = new Program();
            p.simplemethod(j); //calling to simplemethod by passing value 1
            Console.WriteLine(i); //3
            Console.WriteLine(j);//4
            Console.Read();
        }
        public  int  simplemethod(int j) //call it then folow goes to the static method 2
        {
            j = 101;
            return j ;
        }
    }
}

out put is depend upon the local vairable inside the main method.

i=0
j=0


static method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int j = 0;
            Program.simplemethod(j); //calling to simplemethod by passing value 0
            Program.simplemethod(i);//calling to simplemethod by passing value 0
            Console.WriteLine(i); //3
            Console.WriteLine(j);//4
            Console.Read();
        }
        public  static void  simplemethod(int j) //call it then folow goes to the static method 2
        {
            j = 101;
           
        }
    }

}

o/p

i=0
j=0

Hence we can say easily whenever we call a method from the main method either static or non static inside the main method giving priority to the local variable or parameters .


try this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int j = 0;
            Program.simplemethod(j); //calling to simplemethod by passing value 0
            Program.simplemethod(i);//calling to simplemethod by passing value 0
            Console.WriteLine(i); //3
            Console.WriteLine(j);//4
            Console.Read();
        }
        public  static void  simplemethod(int j) //call it then folow goes to the static method 2
        {
            j = 101;
            Console.WriteLine(j);
            Console.Read();
         
           
        }
    }
}





1> value parameter 

Here i and j both are point to the same memory location through ref keyword.. we are giving reference to point out the same memory location and its pass the value the the main mehod where we are calling the ref method. 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cshap
{
    class Program
    {
        public static void simplemethod(ref int j) //call it then folow goes to the static method 2
        {
            j = 101;
            Console.WriteLine(j);//j=101,j=101
            Console.Read();
        }
     
        static void Main(string[] args)
        {
            int i = 0; //initialize i=0
            int j = 0; ////initialize j=0
            simplemethod(ref j); //calling to simplemethod by passing value 0
            simplemethod(ref i);//calling to simplemethod by passing value 0
            Console.WriteLine(i); //3 i=101
            Console.WriteLine(j);//4 i=101
            Console.Read();
        }
     
    }
}


Out parameters : when we want return more than one value from the single method on this scenario we can use out parameter.

suppose we have a one method here we have some digit and we want to calculation of each and every record of an employ the we can use ..you can modify it as per your requirement. 

EX :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cshap
{
    class Program
    {
        public static void Employrecord(int empid, int annual, int Salary, out int Total_Salaryemp, out int empmob) //call it then folow goes to the static method 2
        {
            int i=empid;
            annual = 12;
         
                Total_Salaryemp = Salary * annual;
                empmob = 1234567890;
                try
                {
                    if (i == 0)
                    {
                        Console.WriteLine("Plese enter valid employee id");

                    }
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    Main();
                }


        }
        static void Main()
        {
            int Salary = 12000;
            int Total_Salaryemp;
            int empmob = 0;
            Console.WriteLine("Plese enter employee id ");
            int empid= int.Parse(Console.ReadLine());
            Employrecord(empid, 12, Salary, out Total_Salaryemp, out empmob);
            Console.WriteLine("Employee Mobile = {0} && Employee total salary = {1}", empmob, Total_Salaryemp);
            Console.Read();
         
         

        }
     
    }

}


Parameter array : this method is basically used for an array

Ex:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace traning
{
    class Program
    {
         public static void Parammethod(int[] array1,int[] array2)
        {
            foreach (int number in array1)
            {
                Console.WriteLine(number);
            }
            foreach (int number in array2)
            {
                Console.WriteLine(number);
            }
            Console.Read();
        }
        static void Main(string[] args)
        {
            int[] array1 = { 0, 1, 2, 3 };
            int[] array2 = { 4, 5, 6, 7 };
            Parammethod(array1,array2);
        }
    }
}

Note , we can pass 2 different array in a single method means cant pass string and integer array ,
           It is the last key or statement inside the argument method 
       
      
























Comments

Popular posts from this blog

Prime Number Program in C#

Fibonacci Series in C#

view in sql server