I found a class similar to the follow one:
class Controller
{
private readonly IDataContext _myContext = new DataContext("connectionstring");
public Controller(IDataContext context){
_myContext = context;
}
}
Given instances are created as bellow:
var controller = new Controller(new DataContext("anotherconnectionstring"));
What I would like to know is which will be the final instance assigned to _myContext field? The one passed as an argument or the one used as RHS in the declaration?
All fields which are initialized explicitly in a class definition are moved into the default class/type constructor which will be called before any other explicitly defined parametrized constructor. So final value willl be a value you are passing in custom constructor.
MSDN, Fields (C# Programming Guide)