This is similar to What happens when you run a program?, but not a dupe.
Let's say I have a simple console program with two methods Aand B.
public static void RunSnippet()
{
TestClass t = new TestClass();
t.A(1, 2);
t.B(3, 4);
}
public class TestClass
{
public void A(int param1, int param2)
{
//do something
C();
}
private void C()
{
//do
}
public bool B(int param1, int param2)
{
//do something
bool result = true;
return result;
}
}
Can someone explain in detail (but please keep it in simple plain English), what really happens when RunSnippet calls method A and method B (and they internally call some other methods). I want to understand what really happens under the hood...meaning how are params passed, where are they stored, what happens to local vars, how are return values passed, What will happen if another thread starts running when A has called C, what will happen if an exception is thrown.
I'm not quite sure what level of detail you're looking for, but here's my stab at explaining what's happening:
Note: Since your methods are very small, modern compilers will often "inline" them instead of making a classic call. Inlining means taking the code from the methods and injecting them straight into the main() method rather than going through the (slight) overhead of making a function call.
Given your example I don't see how threading could come into the picture directly. If you were to start the executable a second time, it would run in a new process. That means it would get it's own code segment, data segment and stack segment completely isolating it from the first process.
If your code were run inside a larger program that called main() on several threads, it would run almost exactly as previously described. The code is thread safe because it doesn't access any potentially shared resources such as static variables. There is no way that Thread 1 could "see" Thread 2 because all key data (values and pointers to objects) is stored on the thread's local stack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With