C# Cannot access non-static member field in static context, without actually being in static context

后端 未结 2 1064
名媛妹妹
名媛妹妹 2020-12-12 04:19

Can anyone explain why I get the error \"Cannot access non-static field wtf in static context, even though I am NOT in a static context.

I get the error on the line

2条回答
  •  一个人的身影
    2020-12-12 05:23

    You are assigning the variable to the value of a property within another variable. Your variable wtf is the non-static, however the variable assignment within a class is static. So the assignment, 'public int variable = wtf.queuePosition;', is within a static context.

    One obvious reason this is not allowed is shown within your example. Your code would have a stack overflow exception real quickly. As each instance of Test is newing up another instance of Test, which will new up another ...

提交回复
热议问题