-3

I'm reading WebHostBuilder.cs class.

It's not clear to me why private readonly IHostingEnvironment _hostingEnvironment;

was instanciated in the constructor as _hostingEnvironment = new HostingEnvironment();

Here is the (modified for succinctness) code:

public class WebHostBuilder
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public WebHostBuilder()
    {
        _hostingEnvironment = new HostingEnvironment();
    }
}

UPDATE

I found this question useful. Cast interface to its concrete implementation object or vice versa?

Community
  • 1
  • 1
datkom
  • 1,472
  • 1
  • 11
  • 11
  • readonly just means, that there is not accessability in modifying this member. so if a class has an readonly member of a custom type with a name, then you can set the name to a new value from a instance of the class with the readonly meber but you **can NOT** assign the readonly member itselfe to a new value – Radinator Oct 10 '16 at 10:37
  • 6
    What exactly is not clear? What was your expectation from this code instead? – Andrei Oct 10 '16 at 10:38
  • 1
    I don´t understand the question neither. Where *else* did you expected the value to be set? I guess you *have* to set a value if you want (and you definitly want) to avoid a NullReferenceException afterwards when accessing the field. – MakePeaceGreatAgain Oct 10 '16 at 10:39
  • 2
    Guys I think he is asking why a variable of type ```IHostingEnvironment``` is instantiated with ```HostingEnvironment```? Perhaps he needs to read up on interfaces and concrete types. – Kevin Lee Oct 10 '16 at 10:40
  • Or are you confused why the initialization doesn´t happen just in place of the declaration, so where you declare the field as `private readonly IHostingEnvironment _hostingEnvironment = new HostingEnvironment();` – MakePeaceGreatAgain Oct 10 '16 at 10:42
  • `readonly` simply means that the member can only be assigned in the constructor of the class or on the member itself. What exactly is the question? – Geoff James Oct 10 '16 at 10:42
  • @GeoffJames - Or directly on the field itself. – Enigmativity Oct 10 '16 at 10:45
  • Ah, good catch @Enigmativity - that, too. Thanks - edited :) – Geoff James Oct 10 '16 at 10:46

1 Answers1

4

I guess you´re confused that the value is set in the constructor instead where you declare the field. In your example you could easily and probably also validly write the following:

 private readonly IHostingEnvironment _hostingEnvironment = new HostingEnvironment();

This will have exactly the same effect, as it is implicitly converted to a constructor-statement by the compiler. So both are similar.

However there exists a need to use the constructor when you don´t know the value to be used at compile-time. Imagine the constructor of HostingEnvironment expects an integer to be passed:

public class WebHostBuilder
{
    private readonly IHostingEnvironment _hostingEnvironment // = new HostingEnvironment(whatToSetHere ???);

    public WebHostBuilder(int i)
    {
        _hostingEnvironment = new HostingEnvironment(i);
    }
}

During compile-time you don´t know the value to be provided for the HostingEnvironment-constructor. So you provide it as early as you get it, in this case in the constructor of your own class.

In addition the readonly-modifier simply states that you can set the value for a member only within the constructor (or in place with the declaration, which is converted to a constructor-call, remember what I´ve mentioned above).

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Is it the same with `private readonly HostingEnvironment _hostingEnvironment = new HostingEnvironment();` why the interface `IHostingEnvironment` is set as `new HostingEnvironment();` – datkom Oct 10 '16 at 11:12