Why use readonly to instantiate?

Asked

Viewed 156 times

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?

1 answer

5


The readonly is just an access modifier like everyone else. It has no magic.

This access modifier applies to fields created in a class. When using it, the field can only have a value assigned to it in the code that is inside the builder class.

For example, the code below will generate a compiler error.

private readonly IMyService _myService;

public HomeController(IMyService myService)
{
    _myService = myService;
}

public ActionResult AlgumMetodoQualquer()
{
    _myService = new Service();
    // ^ Isso não é possível, porque o campo é readonly
}

On the other hand, the code compiles normally when the modifier is removed.

I find this useful in cases like the example you showed in the question, where a service is injected. If it is injected, the field does not need and can’t have its value reassigned.


Answering the items: - note that redundancy is purposeful

When to use readonly?

When you want the variable not to have its value reallocated in any part of the class other than the constructor.

There is gain of something using it?

There is gain of the possibility to be sure that the variable will not have its value reallocated in any part of the class other than the constructor.

There is loss of something using it?

Loss of possibility to reallocate the value of the variable to any part of the class other than the constructor.

Why use?

To avoid the value of the variable being reallocated to any part of the class other than the constructor.

Why not use?

To avoid that the value of the variable is reallocated to any part of the class other than the constructor.

  • 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" ;)

  • 1

    @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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.