c# - Why can't a member field have a field initializer call a member function? -


I tried to create a similar area within an MVC controller:

  Func & lt; MyModel, ViewResult & gt; ModelResult = (model) = & gt; View ("myview.cshtml", model);  

This is the result of a compilation error

An object reference is needed for a non-static field, method, or property 'System.Web.Mvc This code works fine as a method

  Private View Result Model Result (MyMold Model).   {Return view ("MyView .cshtml", model); }  

This works fine even when the field started by the consultant

  Public MyController () {ModelResult = (model) = & gt; View ("myview.cshtml", model); }  

Why is the field initiator considered as a constant reference?

Field initializers run in the reverse order of the constructors before the constructor. That is, they go from the most derived type to the least derivative type, and at least the derivative types of fields will be started before the name of any consultant. The decision of a design was not allowed to be referred to this prior to the constructor of a base type, or perhaps more commonly the reference of the partially created frequency from the field initiator was not allowed.

I think this is a sensible decision. If you are not familiar with the language, then the position of the object when a field initial driver is least obvious when the constructor runs. The way the Constructor Announcement is called Flow Productions, it reflects the actual order, so it is easy to argue about the state. Specifically, : base () appears in front of the call constructor body, which means that the base constructor is already running from the time you entered the body. You can not easily estimate the state of the object on a field declaration site, and in fact the field initialization order is different from Java, which was the mainstream language with debate at the beginning of C #.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -