Why is it useful to access static members “through” inherited types?

后端 未结 2 917
故里飘歌
故里飘歌 2020-12-29 06:55

I\'m glad C# doesn\'t let you access static members \'as though\' they were instance members. This avoids a common bug in Java:

Thread t = new Thread(..);
t.         


        
相关标签:
2条回答
  • 2020-12-29 07:14

    I'd agree this is a misfeature. I don't know how often someone on Stack Overflow has posted code of:

    ASCIIEncoding.ASCII
    

    etc... which, while harmless in terms of execution is misleading in terms of reading the code.

    Obviously it's too late to remove this "feature" now, although I guess the C# team could introduce a super-verbose warning mode for this and other style issues.

    Maybe C#'s successor will improve things...

    0 讨论(0)
  • 2020-12-29 07:16

    This is useful in WinForms.

    In any control or form, you can write MousePosition, MouseButtons, or ModifierKeys to use the static members inherited from Control.

    It's still debatable whether it was a good decision.

    0 讨论(0)
提交回复
热议问题