Q:

Public Members vs Constructor of a Class in C#?

I have a doubt.
1. namespace JIMS.ViewModel.Stock
2. {
3.     internal class StockGroupViewModel : JIMS.ViewModel.BaseViewModel
4.     {
5.         JIMSEntities dbContext = new JIMSEntities();
6. 
7.         public StockGroupViewModel()
8.         {                     
9.          dbContext = new JIMSEntities();
10.        }
11.    }
12. }

I have this class. And i want to know which is called first.
when i create an instance of this class
StockGroupViewModel s = new StockGroupViewModel();

Line 5 or Line 9.

A:

Line 5 - it's a field initializer which is executed before any code within the constructor.
From the spec:

10.5.5.2 Instance field initialization
The instance field variable initializers of a class correspond to a sequence of assignments that
are executed immediately upon entry to any one of the instance
constructors (§10.11.1) of that class. The variable initializers are
executed in the textual order in which they appear in the class
declaration. The class instance creation and initialization process is
described further in §10.11.

