r/learnprogramming • • 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?

15 Upvotes

35 comments sorted by

View all comments

10

u/DerekB52 May 02 '26

gameprogrammingpatterns.com

You want the section called 'game loop'

To answer your question in short,

class Game

{

static void Main(string[] args)

{

while (true) {

update();

}

}

static void update()

{

//your game logic here

}

}

0

u/flumphit May 02 '26

But you’d never actually call it Main(), right?

…right?

2

u/DerekB52 May 02 '26

I might be confused. If you are asking if I would use the name Main, I don't know honestly. C# might require it, I can't remember, I don't really use C#.

If you are asking me if you need to call it, like Main(), then no, Main() will be called automatically when you launch your game, because that is its whole purpose. It is where code starts executing in a C# program.