Saturday, June 28, 2014

C# Tutorial for Beginners 13 - Passing Reference , Output and params Pa...





Passing Reference Parameter 



using System;
namespace MyProject.Examples
{
    class ExampleOne
    {
        public static void Main()
        {
            int y = 0;
            MyFunc(ref y);

            Console.WriteLine(y);


            Console.ReadKey();
        }

        public static void MyFunc(ref int x)
        {
            x = 10;
        }

    }
}






Passing Output Parameter 



using System;
namespace MyProject.Examples
{
    class ExampleOne
    {
        public static void Main()
        {
            int a=45,b=67;
            int sum = 0;
            int product = 0;
            MyCalc(a, b, out sum, out product);

            Console.WriteLine("Sum = {0} and Product ={1}", sum,product);


            Console.ReadKey();
        }

        public static void MyCalc(int x, int y, out int sum, out int prod)
        {
            sum = x + y;
            prod = x * y;
             
        }

    }
}






Passing params Parameter 



using System;
namespace MyProject.Examples
{
    class ExampleOne
    {
        public static void Main()
        {
            string[] myArray = new string[4];
            myArray[0] = "name1";
            myArray[1] = "name2";
            myArray[2] = "name3";
            myArray[3] = "name4";

            ArrayMethod();
            //or
            ArrayMethod(myArray);
            //or
            ArrayMethod("n1","n2","n3");

            Console.ReadKey();
        }

        public static void ArrayMethod(params string[] names)
        {
           foreach(string name in names)
           {
               Console.WriteLine(name);
           }
           Console.WriteLine("Array Size = {0}", names.Length);
             
        }

    }
}


















------------------------------------------------------------------

Searches related to method output parameters and return in c#

c# method return 2 variables

c# - How to pass a single object[] to a params object[]

C# Passing arguments by default is ByRef instead of ByVal

Searches related to pass by params parameters in c#

c# pass variable number of arguments

c# pass arguments by reference

 c# pass arguments

c# pass arguments to main

c# timer pass arguments

c# pass arguments to thread

pass arguments to exe c#

c# pass arguments to process

How are parameters passed in C#

C# Parameter Passing, Ref and Out

c# method return multiple variables

c# stored procedure output parameters

return output parameter stored procedure c#

c# call stored procedure with output parameters

c# function output parameter

Passing Parameters

Passing Objects By Reference or Value in C#

No comments:

Post a Comment