VisualBuilder
  Home > Csharp > Tutorials > Parameter Passing in C sharp - C# Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
C# Tutorial Home
1 . Introduction to C sharp
2 . Control Statement: Selection Statement in C sharp
3 . Control Statements – Loops Statement
4 . Methods
5 . Namespaces
6 . Introduction to Classes:
7 . Inheritance:
8 . Polymorphism
9 . Properties
10 . Indexers
11 . Structs
12 . Interfaces
13 . Delegates:
14 . Exception Handling:
15 . Attributes
16 . Enums
17 . Encapsulation
18 . Parameter Passing in C sharp
19 . Method Overloading
20 . Database Interaction Using C sharp
21 . Operator Overloading in C sharp -1
22 . Operator Overloading in C sharp -2
23 . Operator Overloading in C sharp -2
24 . Sockets
25 . DNS [Domain Name System]
26 . Working with Files
27 . Generating Help File in C sharp
28 . Code Access Security
29 . Multi-Threading
30 . Globalization and Localization -1
31 . Working with Registry in C sharp
32 . Globalization and Localization -2
33 . Windows Service
34 . Web Service
35 . Consuming Web Services
36 . Creating Proxy Object of Web Service
37 . Creating an XML document
38 . Reading XML document in C sharp
39 . Using XMLWriter class to Write XML document in C sharp
40 . Assembly Information : Getting Permission set of the assembly
41 . Creating your own Permission Set
42 . Using C sharp Socket
 
Csharp Group Home
Csharp Discussion (7)
Csharp Members (2250)
Csharp Resources
Csharp Source Code (35)
Csharp Articles (0)
Csharp Blogs
Csharp Jobs
Csharp Components (5)
Csharp Books
Csharp Websites (25)
Csharp News (17)
Csharp Q & A (4)
- Csharp Ask Question
- Csharp Questions
- Csharp Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Csharp Tutorial
 Parameter Passing in C sharp
  << Prev: Encapsulation Next: Method Overloading >>

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


 


 


 


 


  << Prev: Encapsulation Next: Method Overloading >>
Csharp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
VoIP Internettelefonie DE
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising