(this == null) in C#!

前端 未结 6 1301
一个人的身影
一个人的身影 2020-12-12 11:52

Due to a bug that was fixed in C# 4, the following program prints true. (Try it in LINQPad)

void Main() { new Derived(); }

class Base {
    pu         


        
6条回答
  •  不知归路
    2020-12-12 12:15

    This observation has been posted on StackOverflow in another question earlier today.

    Marc's great answer to that question indicates that according to the spec (section 7.5.7), you shouldn't be able to access this in that context and the ability to do so in C# 3.0 compiler is a bug. C# 4.0 compiler is behaving correctly according to the spec (even in Beta 1, this is a compile time error):

    § 7.5.7 This access

    A this-access consists of the reserved word this.

    this-access:

    this
    

    A this-access is permitted only in the block of an instance constructor, an instance method, or an instance accessor.

提交回复
热议问题