I work with Ioc Autofac. I can not understand what is happening in this passage of code.

private ISettingService _settings; public ImportSettings(ISettingService settings) { _settings = settings; } 

I create a field and then in the constructor I do things that are incomprehensible to me.

What's going on here?

  • four
    and what is not clear in the assignment of the field value of the parameter? - Grundy
  • in the constructor, we assign the value of the settings parameter to the _settings field, and from where does the settings field get its value? - a.tarasevich
  • but where does the settings field get value from? - this is not a field, but a parameter , and oddly enough it is passed when calling this constructor :-) - Grundy
  • The answer to all your questions I have given below. - Alexander Puzanov

2 answers 2

This code snippet is not affiliated with Autofac. This shows the private _settings field, which is initialized in the ImportSettings constructor specified below, passed to it by the settings parameter.

    When you create an instance of a class, when the custom constructor ImportSettings () is called, the private _settings field will be assigned a default value. The default value is taken from the settings parameter.

    • 2
      Uh, what does your default answer mean? - VladD
    • And what is the task of the designer? Pass the reference to the class instance and assign default values ​​to the fields. - Alexander Puzanov
    • 2
      Well, in C #, the “default” value usually means method parameters that can be omitted: public ImportSettings(ISettingService settings = null) , here the default null value. - VladD
    • When calling the constructor, you can specify a value other than NULL. This will not be considered the default value? - Alexander Puzanov
    • 2
      the default value is still understood as default(T) - user227049