Lambda assigning local variables

前端 未结 4 1814
太阳男子
太阳男子 2021-02-14 01:45

Consider the following source:

static void Main(string[] args)
{
    bool test;

    Action lambda = () => { test = true; };
    lambda();

    if (test)
             


        
4条回答
  •  忘掉有多难
    2021-02-14 02:02

    You are using unassigned variable. Even though the variable is actually assigned, compiler has no way of inferring that from the code you've posted.

    All local variables should be initialized when declared anyway, so this is interesting, but still erroneous.

提交回复
热议问题