Parameter

Method declarations can define a list of parameters to work with.
Parameters are variables that accept the values passed into the method when called.
For example:

void Print(int x)
{
Console.WriteLine(x);
}

This defines a method that takes one integer parameter and displays its value.

Parameters behave within the method similarly to other local variables. They are created upon entering the method and are destroyed upon exiting the method.

Now you can call the method in Main and pass in the value for its parameters (also called arguments):

The Code:

New Project118

The Output of Your Program:

New Project119

The value 50 is passed to the method as an argument and is assigned to the formal parameter x.

You can pass different arguments to the same method as long as they are of the expected type.

The Code:

New Project120.png

The Output of Your Code:

New Project121.png

Prev Lesson:                            Next Lesson:

Introduction of Methods    Multiple Parameters