Information Technology Reference
In-Depth Information
Method Invocations
You can call other methods from inside a method body.
￿The phrases call a method and invoke a method are synonymous.
￿
You call a method by using its name, along with the parameter list, which I will discuss
shortly.
For example, the following class declares a method called PrintDateAndTime , which is
called from inside method Main .
class MyClass
{
void PrintDateAndTime( ) // Declare the method.
{
DateTime dt = DateTime.Now; // Get the current date and time.
Console.WriteLine("{0}", dt); // Write it out.
}
static void Main() // Declare the method.
{
MyClass mc = new MyClass();
mc.PrintDateAndTime( ); // Invoke the method.
}
} Method name Empty parameter list
Figure 5-4 illustrates the sequence of actions when a method is called:
1.
Execution of the current method suspends at that point of the invocation.
2.
Control transfers to the beginning of the invoked method.
3.
The invoked method executes until it completes.
4.
Control returns to the calling method.
Figure 5-4. Flow-of-control when calling a method
Search WWH ::




Custom Search