I have read and relayed this question / answer a few times and I understand the difference between const
and readonly
. I have also read the documentation , but I still can not understand what the gain is I have when using readonly
.
Scenery
I have an application developed in Asp .Net MVC
and in it there is a service layer, I "start" these services through dependency injection by controller
:
private IMyService _myService;
public MeuConstrutor(IMyService myService)
{
_myService = myService;
}
I see some people using private readonly IMyService _myService;
and in one of the latest updates of visual studio
(I can not remember which one), he started suggesting that I add readonly
in those cases. But as I said, I do not understand the real gain of using it.
private IMyService _myService;
vs private readonly IMyService _myService;
- When to use
readonly
? - Is there anything I can use using it?
- Is there anything missing using it?
- Why use?
- Why not use it?