Tuesday, 22 July 2008

Functions in C#

Most functions in C# are the same as other programming languages.

To pass array in C#, it uses params <type>[] <name>

For example,
Define: static int SumVals(parame int[] vals)
Use: SumVals(1,2,3,4,5);

Pass the parameter by reference in C#, ref is the keyword. In this case even without returning the value from the function, the variable also can be changed.

For example,
Define: static void ShowChange(ref int val)
Use: ShowChange(MyVal);

Another similar way to pass the value is using the out keyword. The difference is that,
1. It is illegal to use an unassigned variable as a ref parameter, but it is fine as an out parameter;
2. An out parameter must be treated as an unassigned value;

Main() function can contain the 'string[] args' or have no argument.

No comments: