HI,
Sombir here.
I just want to know that can we use an instant reference or an object to call a static member or static function in c#.
And if yes than How?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 10, 2011, 11:21 AM EDT
C# only allows you to invoke static members from inside the type which defines them (or inherits them) without stating the type name. So if you have class MyClass with static method DoSomething(), then from any other class you must write MyClass.DoSomething() (and you can't invoke it using the instance a as a.DoSomething()). Only from inside MyClass and classes derived from it (including from instance-level methods and properties) you can simply write DoSomething() as if it was an instance-level method. Under the hood, this compiles the same as if called with type name: MyClass.DoSomething().
Zoran
Sam HobbsRank #107 · 20.5K reputationPosted Jul 10, 2011, 6:31 PM EDT
The important thing is that there is only one of a static thing. I would say one instance but everyone insists that it is not an instance, and I think it is confusing for beginners to say that it is not an instance. There is only one object of a static class; always one and only one. So you refer to that one object using the class name.