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

Why methods are needed in C Sharp (C#) - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why methods are needed
Start Program
Write code directly
Code repeats tasks
Hard to fix or change
Use methods to group tasks
Call methods when needed
Code is shorter, easier to fix
End Program
This flow shows how writing code directly can cause repetition and difficulty, but using methods groups tasks for easier reuse and maintenance.
Execution Sample
C Sharp (C#)
using System;
class Program {
  static void SayHello() {
    Console.WriteLine("Hello!");
  }
  static void Main() {
    SayHello();
    SayHello();
  }
}
This code defines a method SayHello and calls it twice to print "Hello!" two times.
Execution Table
StepActionMethod CalledOutputNotes
1Program startsNoMain method begins execution
2Call SayHello()YesHello!First call prints Hello!
3Return from SayHello()NoBack to Main
4Call SayHello() againYesHello!Second call prints Hello!
5Return from SayHello()NoBack to Main
6Main endsNoProgram finishes
💡 Program ends after Main method finishes
Variable Tracker
VariableStartAfter Step 2After Step 4Final
No variables used----
Key Moments - 2 Insights
Why do we call SayHello() twice instead of writing Console.WriteLine("Hello!") twice?
Calling SayHello() reuses the same code, so if we want to change the message, we only update it once inside the method (see execution_table steps 2 and 4).
What happens if we change the message inside SayHello()?
All calls to SayHello() will print the new message, making it easier to fix or update (refer to execution_table steps 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when SayHello() is called the first time?
A"Hello!"
B"Hi!"
CNothing
DError
💡 Hint
Check execution_table row 2 under Output column
At which step does the program finish running?
AStep 4
BStep 6
CStep 2
DStep 3
💡 Hint
Look at execution_table last row Step 6 notes
If we wrote Console.WriteLine("Hello!") twice instead of using SayHello(), what would be a problem?
AIt would print different messages
BProgram would run faster
CCode would be longer and harder to update
DIt would cause an error
💡 Hint
Refer to key_moments about code reuse and fixing
Concept Snapshot
Methods group code into reusable blocks.
Call methods to run the grouped code.
Avoid repeating same code multiple times.
Change code in one place inside method.
Makes program shorter and easier to fix.
Full Transcript
This example shows why methods are needed. Without methods, repeating the same code makes programs longer and harder to fix. Here, the method SayHello prints "Hello!". The Main method calls SayHello twice, so the message prints two times. If we want to change the message, we only update SayHello once. This saves time and avoids mistakes. The execution table shows each step: program starts, calls SayHello, prints message, returns, calls again, prints again, then ends. Using methods helps keep code clean and easy to manage.