Eric Bole-Feysot
Eric Bole-Feysot

Reputation: 15137

What is the difference between App constructor and app.Onstartup?

I'm doing some refactoring on my application and I want to gather global initializations in the app.xaml.cs.

I had some of them in the constructor and other in the OnStartup.

OnStartup seems to be called right after the constructor.

What is the difference between the two methods? Where should I put all my code?

Upvotes: 4

Views: 1369

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157038

There is not much difference, as you can see from the source of System.Windows.Application.

The OnStartup method is the last method called from the constructor.

If you implement your own constructor, the OnStartup already has been fired. Also, in the unique case you would override an own implementation of OnStartup could discard the implementation you already had (if you don't call base).

Personally I would still go for the OnStartup, but I guess it doesn't really matter that much. Mixing the two together though can be dangerous if you don't know the real execution order.

Upvotes: 3

Related Questions