Static Method Parameters issue

G

Guest

Hi,

Mine is a very basic doubt ...

(1) If we are using a static field/object, then the field/object is shared
across all the places in the entire application as it is stored only at one
place.
(2) If it is an instance-based object, then when each instance gets created,
a new memory space gets allocated to every instance.

Now coming to methods,
(3) In the case of instance-based methods, there will be only copy of the
method existing in the memory (yes only one irrespective of the number of
instances using it). This is so because, there will be an instance
intrinsically passed to this instance-based method (we need not specify
this). This implicitly passed parameter is 'this' in C# and 'Me' in VB .NET
which refers to the instance calling that method. The method then operates on
this instance passed.
(4) The case is same in the case of static methods also except that the
implicit 'this' or 'Me' wont be passed. Now for my question: In this static
method, how are the parameters stored ... specifically when multiple objects
calls this instance-based method passing different parameters, then is there
any risk of corruption or how does this work??

Kindly correct me if I'm wrong anywhere...

Thanks in advance.
 
J

Jon Skeet [C# MVP]

Samba said:
Mine is a very basic doubt ...

(1) If we are using a static field/object, then the field/object is shared
across all the places in the entire application as it is stored only at one
place.
(2) If it is an instance-based object, then when each instance gets created,
a new memory space gets allocated to every instance.

Now coming to methods,
(3) In the case of instance-based methods, there will be only copy of the
method existing in the memory (yes only one irrespective of the number of
instances using it). This is so because, there will be an instance
intrinsically passed to this instance-based method (we need not specify
this). This implicitly passed parameter is 'this' in C# and 'Me' in VB .NET
which refers to the instance calling that method. The method then operates on
this instance passed.

What do you mean by "copy of the method"? You need to be *very* clear
as to what you mean by that - I suspect that when you try to explain
it, you'll find the problem goes away.
(4) The case is same in the case of static methods also except that the
implicit 'this' or 'Me' wont be passed. Now for my question: In this static
method, how are the parameters stored ... specifically when multiple objects
calls this instance-based method passing different parameters, then is there
any risk of corruption or how does this work??

Parameters are stored on the stack, whether for instance methods or
static methods. They don't interfere with each other (although if those
parameters are references to other objects, then things become a bit
more complicated, of course).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top