Click here to Skip to main content
15,937,186 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am student .i have a question!.i dont understand "Instance and Static Method with this Keyword"..plz help me .thank you!
PIEBALDconsult 13-Feb-15 20:37pm    
Well I don't understand your question. Please use Improve question to add more detail and context. Do you have a link to what you don't understand?
Nguyen Jay 13-Feb-15 21:01pm    
how to use keyword this ..creat Instance and Static Method .thank you bro!
PIEBALDconsult 13-Feb-15 21:05pm    
That's not any better. Try again, and use Improve question, don't post a reply to me.

1 solution

In brief: instance method gets implicit parameter "this" used to access the instance on which the call is done. Static method does not have it; it is roughly equivalent to the pre-OOP function/procedure; in .NET, a static method cannot be virtual (but there are technologies where it can be).

To illustrate instance method mechanism, consider this:
class MyClass {
   internal void SomeInstanceMethod() { // "this" parameter is not shown, but it's there
       this.SomeMember = 3; // which SomeMember object is this? see below...
   internal int SomeMember { get; set; }

MyClass myInstance = new MyClass();

// when you enter SomeInstanceMethod stack frame in this call,
// this equals to myInstance
// if it was a static method, the call would be equivalent to
// SomeInstanceMethod(myInstance)

As you can see, myInstance.*() call is the way to pass instance parameter "this" to the method.

Share this answer

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