C
C#2w ago
Hampta

✅ Why and How does this work?

using System;

public class Program
{
static void Main(string[] args)
{
Welcome();
}
static void Welcome()
{
Console.WriteLine("Welcome");
Console.WriteLine("I am a method");
Console.WriteLine("End of method");
}
}
using System;

public class Program
{
static void Main(string[] args)
{
Welcome();
}
static void Welcome()
{
Console.WriteLine("Welcome");
Console.WriteLine("I am a method");
Console.WriteLine("End of method");
}
}
why does this work? when i think of code, i think of line to line declaration, so id think Main() runs furst, and then the method. so why is this?
6 Replies
Jimmacle
Jimmacle2w ago
because main calls welcome, so execution jumps to welcome, it runs the code inside, then it jumps back to main where it left off code only executes line by line until it runs into code that tells it to go somewhere else, like a conditional statement, loop, method call, etc.
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2w ago
Get started with beginner tutorials - Interactive text tutorial - Video tutorial - Fundamentals documentation
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle7d ago
there was no reason to close this on them... let's relax on the thread policing, you really shouldn't close threads that have valid questions regardless of whether you think the question is too basic or not especially because they'll never see the answer i gave them, they'll just see that their thread disappeared
Hampta
HamptaOP7d ago
This is C#, If you meant what i used before, it was Lua and even before that, Python ohh so they just like exist? oh

Did you find this page helpful?