0
0
C Sharp (C#)programming~10 mins

Method declaration and calling in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Method declaration and calling
Start Program
Declare Method
Main Method Calls Method
Execute Method Body
Return to Main
Program Ends
The program starts by declaring a method, then the main method calls it, the method runs, and control returns to main.
Execution Sample
C Sharp (C#)
using System;
class Program {
  static void SayHello() {
    Console.WriteLine("Hello, friend!");
  }
  static void Main() {
    SayHello();
  }
}
This code declares a method SayHello and calls it from Main to print a greeting.
Execution Table
StepActionEvaluationOutput
1Program startsNo output
2Method SayHello declaredNo output
3Main method startsNo output
4Main calls SayHello()Call method
5SayHello executes Console.WriteLinePrints stringHello, friend!
6SayHello returns to MainNo output
7Main endsProgram ends
💡 Program ends after Main finishes execution
Variable Tracker
VariableStartAfter CallFinal
No variablesN/AN/AN/A
Key Moments - 2 Insights
Why doesn't the method run when it is declared?
Declaring a method only tells the program what it does. It runs only when called, as shown in step 4 of the execution table.
What happens after the method finishes running?
After the method finishes (step 6), control returns to the place where it was called (Main method), continuing the program.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 5?
A"Hello, friend!"
B"SayHello"
CNothing is printed
D"Main method starts"
💡 Hint
Check the Output column at step 5 in the execution table.
At which step does the program call the method SayHello?
AStep 2
BStep 6
CStep 4
DStep 7
💡 Hint
Look at the Action column to find when SayHello is called.
If we remove the call SayHello() in Main, what changes in the execution table?
AStep 2 disappears
BStep 5 output disappears
CProgram ends earlier at step 4
DNo change at all
💡 Hint
Without calling SayHello, the method body does not run, so no output is printed.
Concept Snapshot
Method declaration syntax:
static void MethodName() {
  // code
}

Call method by name:
MethodName();

Methods run only when called.
After running, control returns to caller.
Full Transcript
This example shows how to declare a method named SayHello and call it from the Main method. The program starts, declares the method, then Main calls SayHello. The method runs and prints "Hello, friend!". After that, control returns to Main, and the program ends. Declaring a method does not run it; it runs only when called. After the method finishes, the program continues from where it left off.