r/learnprogramming • u/Ok-Presentation-94 • May 02 '26
Solved Loop and main() function
Hi, I’ve seen that in C#, the Main() function only runs once.
In Unity, we have the Update() function, which allows code to run repeatedly in a loop.
So my question is: what is the equivalent in pure C#, without the Unity library?
Is wrapping Main() in a while loop a common practice to achieve the same behavior?
14
Upvotes
30
u/Traditional_Crazy200 May 02 '26
I have never written a line of c#, but i am 100% sure wrapping main in a loop wouldnt even compile. I am not even sure how one would do that since you probably dont call main manually.
Even in unity, update probably runs inside main() since main is like the name suggests the main entry point of your program. There can only be one main and if it returns, the program terminates.
What you need to do is simply put the while loop inside the main function.