C# Workshop - Week 4 Review Questions

Started by
9 comments, last by SamLowry 16 years, 8 months ago
Quote:Original post by foolios
So the answer should read:
The name of the class to which they belong, followed by a period.
Right?


The name of the class or struct, yes. Structs can have static members too.


To me it looks that that question was made with C++ or java in mind, which let you access static members through objects:
class SomeClass{    public static void classFunction() { ... }}SomeClass obj = new SomeClass();obj.classFunction(); // works in java, but not in C#

It's a good thing that C# won't let you do this, as the static method simply isn't a member of the object. It's also a bit confusing, as programmers might think there's some sort of runtime dispatching going on, which is not the case.


So to summarize: C# will only let you access static members through the class/struct's name.

This topic is closed to new replies.

Advertisement