2
I read and reread that question/answer a few times and I understood the difference between const
and readonly
. I also read the documentation, but I still can not understand what gain I have using the readonly
.
Setting
I have an application developed in Asp .Net MVC
and in it there is a layer of service(service), I "start" these services through dependency injection by controller
:
private IMyService _myService;
public MeuConstrutor(IMyService myService)
{
_myService = myService;
}
I see some people using the private readonly IMyService _myService;
and, in one of the latest updates of visual studio
(I don’t remember which one exactly) he went on to suggest that I add the readonly
in such cases. However, as said, I do not understand what the real gain of using it.
private IMyService _myService;
vs private readonly IMyService _myService;
- When to use the
readonly
? - There is gain of something using it?
- There is loss of something using it?
- Why use?
- Why not use?
Cool.. The part that had been more doubtful was about the injection. About duplicated, I think it was a case of "changed the color of the grass, starved to death" ;)
– Barbetta
@Barbetta Tranquil, I know how it is. I made it explicit because there are people here on the site who vote negative just seeing that have answer to question dup.
– Jéf Bueno