Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have doubt on what is the need of instance methods access static member.

Generally static members are share to the all instance of the class but why instance

methods access static members.instance methods are associated with instance of class

already static members are shared but why specially and what is need of access static

members are accessed.


please help me

thank u

What I have tried:

I have doubt on what is the need of instance methods access static member.

Generally static members are share to the all instance of the class but why instance

methods access static members.instance methods are associated with instance of class

already static members are shared but why specially and what is need of access static

members are accessed.
Posted
Updated 5-Oct-17 4:16am

1 solution

Static members can be accessed at any time without an instance of the class - that doesn't stop instance methods from accessing static information.
See the answers to your previous - rather similar - question yesterday: When to use static methods C#[^]

Quote:
Thank u but static method perform the operation only static fields or not i.e static methods are access instance fields for perform the operation is possible?


Static methods can only access static fields, properties, methods, etc. - they cannot access instance anything because there is no instance present when they are running: there is no this reference to access instance data via.

The only exceptions to that are when an instance is pass to a static method, or the static method creates it's own instance:
C#
public void DoSomething()
   {
   Console.WriteLine(this.Text);
   }
public static void MyMethod(MyClass mc)
   {
   mc.DoSomething();
   }
Or
C#
public void DoSomething()
   {
   Console.WriteLine(this.Text);
   }
public static void MyMethod()
   {
   MyClass mc = new MyClass();
   mc.DoSomething();
   }
 
Share this answer
 
v2
Comments
Krishna Veni 5-Oct-17 10:26am    
Thank u but static method perform the operation only static fields or not i.e static methods are access instance fields for perform the operation is possible?
OriginalGriff 5-Oct-17 10:36am    
Answer updated
OriginalGriff 5-Oct-17 11:09am    
Would you mind asking that again?
Most of the time I can work out what you mean to say, but from that I have no idea at all!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900