parameters in functions

  • Thread starter Thread starter Serge Blokhin
  • Start date Start date
S

Serge Blokhin

Hi everyone,
I have a newbie question,
Say I have :
-----------------------------
class Class1 {
private int field1;
private string field2;
}

class Class2 {
public bool func1( Class1 cls1){
return (cls1 == null);
}
}
------------------------------

as I understand when func1 gets control cls1 gets cloned? I mean
finally i get 2 identical objects, one inside of function scope and
one outside?
Did I understand it correctly?
Serge
 
Serge Blokhin said:
Say I have :
-----------------------------
class Class1 {
private int field1;
private string field2;
}

class Class2 {
public bool func1( Class1 cls1){
return (cls1 == null);
}
}
------------------------------

as I understand when func1 gets control cls1 gets cloned? I mean
finally i get 2 identical objects, one inside of function scope and
one outside?
Did I understand it correctly?

No. See http://www.pobox.com/~skeet/csharp/parameters.html for a fairly
close look at this kind of thing. Basically it all comes down to
understanding references and reference types.
 
...
class Class2 {
public bool func1( Class1 cls1){
return (cls1 == null);
}
}
as I understand when func1 gets control
cls1 gets cloned? I mean finally i get 2
identical objects, one inside of function
scope and one outside?
Did I understand it correctly?

No you didn't.

The parameter is passed "by value", that is to say that the value of the
parameter is copied into the local variable "cls1".

However, the value of the parameter isn't the object itself, but a
*reference* to it. Hence it is still only one object.

// Bjorn A
 
Bjorn,
If you are right, that means I could change outside object from my
function simply call its methods, but it is not happening. It is only
going to happen if I use ref parameter
Thanks
Serge
 
Jon,
According to your link,
I get another object in the function, right?
so from memory consumption perspective I use twice more memory compare
to if I declared function as:

class Class2{
public bool func1( ref Class1 cls1){
return (cls1 == null);
}
}
Right?
Thanks
Serge
 
Serge said:
Bjorn,
If you are right, that means I could change outside object from my
function simply call its methods, but it is not happening. It is only
going to happen if I use ref parameter

That all depends on what you're doing with that parameter. Any
assignments will have no effect in the calling method as you are
assigning an object reference to a local variable. So this would have no
noticeable effect:

myParameter = new object();

However, using the object's properties and methods will yield a lasting
change if any of those properties or methods modify the object. So this
would have a noticeable effect:

myParameter.Title = "New Title";

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Serge said:
Jon,
According to your link,
I get another object in the function, right?

No. You get a copy of the object reference, not the object.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Serge Blokhin said:
According to your link,
I get another object in the function, right?

No, you get a distinct variable, and both variables have the same value
- but that value is a reference to a single object.
so from memory consumption perspective I use twice more memory compare
to if I declared function as:

class Class2{
public bool func1( ref Class1 cls1){
return (cls1 == null);
}
}
Right?

No. Read the page again, very carefully, paying particular attention to
the early sections. Let me know if it doesn't become any clearer.
 
That's correct.

Then you're probably trying to initialize a new object for the variable,
which results in a *replacement* of the reference you got. If you simply
"work" on the object, any changes still exists in the object when you leave
the method:

static void method1(Class1 cls1)
{
cls1.attribute = "method1";
}

The default behaviour is that the parameter is passed by value, that is that
the value of the parameter is copied into the local variable, but for
objects from classes, that means that the value isn't the object, but the
reference to it.

When you use the ref keyword, you should conceptually think of it as if
you're working with the *same* variables as the one you passed as a
parameter, not a copy at all.

static void method2(ref Class1 cls1)
{
cls1 = new Class1(); // see cls1 as an "alias"
// for the outside *variable*.
cls1.attribute = "method2";
}

If you leave out the ref keyword in example method2, the argumentlist is
really redundant:

static void method3(Class1 cls1)
{
cls1 = new Class1();
cls1.attribute = "method3";
}

is functionally equal to:

static void method3()
{
Class1 cls1 = new Class1();
cls1.attr = "method3;
}

....although the latter avoids the then unnecessary operation of copying the
variable value from the parameter. (Note that the last two examples are
unnecessary altogether, since the effect of them vanishes as they go out of
scope...)

// Bjorn A
 
Back
Top