A method must be created outside other methods including the Main method. Here is an example of how to create a method called PrintHello which will print hello on the screen. The whole program is included to show you where the method must be created.
using System;
class methods
{
private static void PrintHello()
{
Console.WriteLine("Hello");
}
public static void Main()
{
PrintHello();
}
}
The private keyword means that the method can only be called from in the same class. If you used the public keyword in its place then it would be accessible from anywhere. The static keyword must be used because it is going to be called from the main method which is also static. This method has no return value so void must be used. The name of the method follows the return value which in this case is PrintHello. All methods must be followed by brackets. The contents of the method are put between the curly brackets that follow it. Console.WriteLine is used inside the method to print Hello. The PrintHello method is called from the Main method by using its name followed by brackets and a semi-colon to end the statement.
Parameters and return values
Variables can be passed to a method by putting them between the brackets that follow its name. Here is an example of how you would pass 2 integers to a method called Add that adds the 2 values together.
static void Add(int n1, int n2)
{
int answer;
answer = n1 + n2;
}
You would call this method with the values in brackets.
Add(1,2);
We can use the return value of a method to get the answer. Changing void to int will make the method return an integer. You need to use the return keyword at the end of the method to return the value.
static int Add(int n1, int n2)
{
int answer;
answer = n1 + n2;
return answer;
}
You can store the result from the method in a variable by setting the variable equal to the method when calling it.
int total;
total = Add(1,2);
Reference parameters
The methods that we have done so far use pass by value parameters. This means that a copy of the variable passed to the method is used instead of the actual variable. If you change the value of a parameter passed by value it will not change the value of the variable in the method that called it. You must pass parameters by reference by using the ref keyword if you want the variable in the calling method to be changed.The following example has a method called AddOne which takes an int as a parameter and adds 1 to that value. Because it is a reference parameter the original value which in this case is x will have 1 added to it. It is important to use the ref keyword in both the method that is called and the method that calls that method. You also have to initialize the variable that is passed by reference to something before passing it.
using System;
class methods
{
private static void AddOne(ref int i)
{
i = i + 1;
}
public static void Main()
{
int x = 0;
AddOne(ref x);
Console.WriteLine(x);
}
}
Global and local variables
A global variable can be accessed by any method and a local variable can only be accessed by the method it is declared in. When a variable is declared outside of all methods it becomes global. Here is an example that shows that a local variable can only be accessed in its method and that a global variable can be accessed from any method.
using System;
class methods
{
static int MyGlobalVariable;
static void ChangeGlobalValue()
{
int MyLocalVariable;
MyLocalVariable = 4;
MyGlobalVariable = 5;
}
public static void Main()
{
MyGlobalVariable = 7;
ChangeGlobalValue();
}
}
0 comments:
Post a Comment