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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Jul 10, 2011, 10:31 PM
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.
Zoran HorvatPosted Jul 10, 2011, 3:21 PM
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