Parameter Passing in C#

text zoom

A method's parameters are the types that get passed to it when the method is called. The list of parameters begins by specifying zero or more 'fixed parameters', and it may finish by specifying a single parameter-array. This latter element - declared using the 'params' keyword - means that it is possible to pass an arbitrary number of types to a single method.


 


Syntax: [Modifier] parameter-type parameter-identifier



  • [Modifier]: This is optional and can be either 'ref' or 'out'.

  • Parameter-type: This declares the type of the parameter.

  • Parameter-identifier: The name of the parameter.


 


There are 3 type of parameter that are passed to the method



  • Passing by Value

  • Passing by Reference

  • ‘Output' Parameter.


 


 


Passing By Value:


The parameter modifiers 'ref' and 'out' relate to how the parameter is passed into the method. Where neither of these modifiers is used, the parameter is passed in 'by value'. By default we are passing variable by value. Here the new copy of the variable is send to the called method. So changes done to the variables are not reflected on the actual parameter.


 


Syntax:


passValueByValueType(_initialValue); //Call a method


private void passValueByValueType( int iV)


{ iV = 20; }


 


Passing By Reference:


In C# we can pass variables into methods 'by reference'. Where a variable is passed by reference, the 'ref' modifier must be used both in the method head and the method invocation (illustrated by the next code block). The changes made to the formal argument will reflect on the actual argument.


 


Syntax:


passValueByReferenceType( ref _initialValue); //Calling Method


private void passValueByReferenceType( ref int iV)


{ iV = 20; }


 


 


‘Output' parameter:


Where a method parameter is defined (and invoked) using the 'out' modifier, it is passed by reference. The difference between the 'out' and the 'ref' modifier is this: a parameter modified by the 'out' keyword need not be assigned a value before being passed into the method, but must be assigned a value in the method.


 


Syntax:


passValueByOutParameter( out _initialValue, out _bValue);//Calling


   


‘Params' Modifier


One can pass an arbitrary number of types to a method by declaring a parameter array with the 'params' modifier.


 


Syntax:


public int getTheTotalUsingParam( params int [] intArr)


 


Example: Demonstrate Parameter Passing in C#


 





 


 


Form18.cs:


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


 


namespace cSHARPEXAMPLES


{


public partial class Form18 : Form


{


 public Form18()


 {


    InitializeComponent();


 }


 private void btnValue_Click( object sender, EventArgs e)


  { //Value


    int _initialValue = Int16 .Parse(textBox1.Text);


  MessageBox .Show( "Before Calling " +


    _initialValue.ToString());


    passValueByValueType(_initialValue);


    textBox1.Text = _initialValue.ToString();


  MessageBox .Show( "After Calling " +


    _initialValue.ToString());


  }


 private void btnReference_Click( object sender, EventArgs e)


    { //Reference


        int _initialValue = Int16 .Parse(textBox2.Text);


    MessageBox .Show( "Before Calling " +


        _initialValue.ToString());


        passValueByReferenceType( ref _initialValue);


        textBox2.Text = _initialValue.ToString();


    MessageBox .Show( "After Calling " +


        _initialValue.ToString());


    }


 


 private void btnOut_Click( object sende    r, EventArgs e)


    { //Out


        bool _bValue;


        int _initialValue = Int16 .Parse(textBox3.Text);


    MessageBox .Show( "Before Passing " +


        _initialValue.ToString());


        passValueByOutParameter( out _initialValue, out _bValue);


        textBox3.Text = _initialValue.ToString();


    MessageBox .Show( "After getting the value " +


        _initialValue.ToString());


    }


 private void passValueByReferenceType( ref int iV)


    {


        iV = 20;


    }


 private void passValueByValueType( int iV)


    {


        iV = 20;


    }


 private void passValueByOutParameter( out int iV, out bool bValue)


    {


        bValue = false ;


        iV = 2;


    if (iV > 40)


            bValue = true ;


    else


            bValue = false ;


        iV = iV * 20;


    }


 }


}


 


OutPut:


 





 


Clicking On:- Passing Parameter By Value


 


 


 


Clicking On:- Passing Parameter By Reference


 


 


 


Clicking On:- Passing Parameter By Out


 


 


 


 

                    

Copyright © 2010 VisualBuilder. All rights reserved