这个内部类静态范围魔法如何工作?

gin*_*boy 1 c# static scope

public class Outer
{
    public  class Inner
    {
        public static string OtherValue { get { return SomeValue; } }
    }

    public static string SomeValue { get { return "Outer"; } }
}
Run Code Online (Sandbox Code Playgroud)

为什么以上编译?SomeValue是否超出内部范围且需要符合资格Outer.SomeValue?上面的内容与下面的内容基本相同(不会编译)?

public class Outer
{
    public class Inner
    {
        public static string OtherValue { get { return Outer.SomeValue; } }
    }

    public static string SomeValue { get { return "Outer"; } }
}
Run Code Online (Sandbox Code Playgroud)

static这里有什么魔力?

Joe*_*Joe 7

它像是:

public class Super
{
    public class Sub
    {
        public static string OtherValue { get { return Super.SomeValue; } }
    }

    public static string SomeValue { get { return "Outer"; } }
}
Run Code Online (Sandbox Code Playgroud)

随着Super控股静SomeValue


它更清晰地显示如下:

public class Super
{
    public static string SomeValue { get { return "Outer"; } }
}

public class Sub
{
    public static string OtherValue { get { return Super.SomeValue; } }
}
Run Code Online (Sandbox Code Playgroud)

现在用SubSuperpublic他们的静态属性是所有可见他们共同的范围之内.正如@jonskeet所指出的那样,编译器将首先搜索最适合SomeValue查找类的内容,Sub因为该值在该类中使用.

现在这样的东西不会编译:

public class Super
{
    private static string SomeValue { get { return "Outer"; } }
}

public class Sub
{
    public static string OtherValue { get { return SomeValue; } }
}
Run Code Online (Sandbox Code Playgroud)

然而,

public class Super
{
    private static string SomeValue { get { return "Outer"; } }

    public class Sub
    {
        public static string OtherValue { get { return SomeValue; } }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于课程的嵌套,上述情况很好.