Skip to content

Parameters

In Part 2 we saw how to use parameters within our functions and procedures. In C# your methods can also have parameters. These are mostly the same as in C/C++ but the syntax for pass by reference does differ.

Parameters Why, When, and How?

You will use parameters in C# in the same way you did in C/C++. These allow you to pass values in to methods, and with pass by reference you can change the value of the variable that is passed to it.

In C

Example

See the examples in the static method’ page. The following example includes a parameter passed by reference. Notice that you also have to use the ref keyword in the method call as well. This helps highlight the fact that the value is being passed by reference, and that the passed variable’s value may be changed in the call.

class MyProgram
{
public static void ChangeName(ref string name)
{
name = "Other Name";
}
public static void Main()
{
string name = "First Name";
ChangeName(ref name);
}
}