Parameters

I l @ ve RuBoard

Another way to define variables is through the use of formal method parameters. Table D.1 outlines the four types of method parameters.

Table D.1. Parameter Types in C#
Parameter Type Modifier Description
value none Modifications do not impact original argument passed into the method.
reference ref Modifications directly impact original argument passed into the method.
output out Used for passing values out of a method. Initial value is inconsequential.
parameter array params Enables variable length parameter lists.

Consider the following class, which employs each of the parameter types:

  using System; class Test {     public void ValueParamMethod(int a)     {         a += 2; // the original parameter is not modified     }     public void RefParamMethod(ref int a)     {         a += 2; // the original parameter is not modified     }     public void OutParamMethod(out int a)     {         a = 2; // original value unimportant, a set to 2 and passed out     }     public void ParamArrayMethod(params int[] args)     {         // provides flexibility through indefinite number of arguments     } } 
I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net