Reputation: 113452
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.sleep(..); //Probably doesn't do what the programmer intended.
On the other hand, it does let you access static members 'through' derived types. Other than operators (where it saves you from writing casts), I can't think of any cases where this is actually helpful. In fact, it actively encourages mistakes such as:
// Nasty surprises ahead - won't throw; does something unintended:
// Creates a HttpWebRequest instead.
var ftpRequest = FtpWebRequest.Create(@"http://www.stackoverflow.com");
// Something seriously wrong here.
var areRefEqual = Dictionary<string, int>.ReferenceEquals(dict1, dict2);
I personally keep committing similar errors over and over when I am searching my way through unfamiliar APIs (I remember starting off with expression trees; I hit BinaryExpression.
in the editor and was wondering why on earth IntelliSense was offering me MakeUnary
as an option).
In my (shortsighted) opinion, this feature:
(IMO, operators are a special case that warrant their own discussion.)
Given that C# is normally a "pit of success" language, why does this feature exist? I can't see its benefits (other than 'discoverability', which could always be solved in the IDE), but I see lots of problems.
Upvotes: 18
Views: 1252
Reputation: 887837
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.
Upvotes: 2
Reputation: 1502526
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...
Upvotes: 8