Question Asking about the order of the method executing

ChinhTD

New member
Joined
Nov 2, 2024
Messages
2
Programming Experience
1-3
I'm super new to OOP, the learning path is interesting, and easy with the help from great communities like this one or geeks.. until this barrier which I tried to google for a day, maybe because of my stupidity, or everyone knows it already :(. Let's say I have methodX calls to method1 then calls to methodA, method1 calls to method2, method2 calls to method3, and methodA calls to methodB, methodB calls to methodC. My question is: in C#, the method3 needs to be finished before methodA is called "strictly" or not. And the same question, but a little bit different case: methodX calls the same methodM from an array of object, then another methodP. In this case, when the methodP execute? Thank you for your time and wisdom!
 
Your question not even about object oriented programming. It's regarding standard procedural programming.

Methods (otherwise called functions, procedures, or subroutines in other programming languages) are executed in a linear sequential manner (until you start playing with asynchronous code). Execution proceeds from the entry point for the code, then runs sequentially. If it encounters a method call, execution transfers into that method. At the end of the method, execution transfers back into the calling method.

Try playing with simple code in .NET Fiddle:
C#:
using System;
                    
public class Program
{
    static void Inner()
    {
        Console.WriteLine("Entering Inner()");
        Console.WriteLine("Leaving Inner()");
    }

    static void Outer()
    {
        Console.WriteLine("Entering Outer()");
        
        Inner();
        
        Console.WriteLine("Leaving Outer()");
    }
    
    public static void Main()
    {
        Console.WriteLine("Entering Main()");
        
        Outer();
        
        Console.WriteLine("Leaving Main()");
    }
}

which produces the following output:
Code:
Entering Main()
Entering Outer()
Entering Inner()
Leaving Inner()
Leaving Outer()
Leaving Main()
 
I appreciate your wisdom! I tried that but I was not sure that it's strict as a rule or so. With your answer, I've got it as a rule. How about the second part of the question? It's harder for me to be sure, or even testing.
Your question not even about object oriented programming. It's regarding standard procedural programming.
You are so right about this, my bad. I was thinking about OOP because of the second part of the question, though.
 
Methods on objects follow the same rules in terms of execution. Until you start working with asynchronous code that runs on multiple threads, there is only on thread that is executing code. There is only one linear path of execution through the code.

.NET Fiddle:

C#:
using System;

class Foo
{
    int _id;

    public Foo(int id)
    {
        _id = id;
        Console.WriteLine($"Creating Foo{_id}");
    }

    public void Inner()
    {
        Console.WriteLine($"Entering Foo{_id}.Inner()");
        Console.WriteLine($"Leaving Foo{_id}.Inner()");
    }

    public void Outer()
    {
        Console.WriteLine($"Entering Foo{_id}.Outer()");
        
        Inner();
        
        Console.WriteLine($"Leaving Foo{_id}.Outer()");
    }
}
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Entering Main()");
        
        Foo [] foo = new Foo[5];
        for(int i = 0; i < foo.Length; i++)
            foo[i] = new Foo((i + 1) * 10);
        
        for(int i= 0; i < foo.Length; i++)
            foo[i].Outer();
        
        Console.WriteLine("Leaving Main()");
    }
}

Code:
Entering Main()
Creating Foo10
Creating Foo20
Creating Foo30
Creating Foo40
Creating Foo50
Entering Foo10.Outer()
Entering Foo10.Inner()
Leaving Foo10.Inner()
Leaving Foo10.Outer()
Entering Foo20.Outer()
Entering Foo20.Inner()
Leaving Foo20.Inner()
Leaving Foo20.Outer()
Entering Foo30.Outer()
Entering Foo30.Inner()
Leaving Foo30.Inner()
Leaving Foo30.Outer()
Entering Foo40.Outer()
Entering Foo40.Inner()
Leaving Foo40.Inner()
Leaving Foo40.Outer()
Entering Foo50.Outer()
Entering Foo50.Inner()
Leaving Foo50.Inner()
Leaving Foo50.Outer()
Leaving Main()
 

Latest posts

Back
Top Bottom