Corner case in using lambdas expression in base constructor

后端 未结 5 1530
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 08:03

In the Framework we are building we need the following pattern:

public class BaseRenderer
{
    Func renderer;
    public BaseRenderer(Func<         


        
5条回答
  •  不知归路
    2021-01-05 08:43

    The lambda captured the value of "this" and captured null since the object wasn't constructed yet. This strikes me as a compiler bug, it should have generated an error for this. Code like this normally generates a CS0027 (keyword 'this' is not available in the current context) or CS0120 (an object reference is required). I bet that isn't easy to implement.

    Anyhoo, the code cannot work. The NameRenderer class needs a constructor with a string argument so it can initialize the base class.

提交回复
热议问题