Reference Types and Value Types

D

daniel

This is a pretty basic-level question, but I'd really like to know, so
thanks for any help or pointers you can provide (like what I would
google for ;o)

Suppose:

<code>

myFunc()
{
SomeType myType = new SomeType;
Console.Write(myType.MyProperty);
bool whatever = myOtherFunc(myType);
}

myOtherfunc(SomeType passedType)
{
Console.Write(myType.ToString())
return true;
}

</code>

My question: since myType is a reference type, only one instance of this
object is ever created in memory, right? The Lead Software Architect
here (C++, no C# experience) was telling me about passing by reference--
is this implicitly done by reference types in C#? Also, could I pass by
value? Why would I want to?

All these questions are asked under the auspices of least amount of
overhead. My primary concern is good coding practices (and learning).

Thank you,
-daniel
 
L

Lateralus [MCAD]

daniel,
To my knowledge you can't pass a "reference" type by value if you wanted
to. It is automatically passed by reference. If you have a value type and
you want to maintain it's value across method calls, you need to pass it by
reference.

example should print out 12:

private void Test()
{
int index = 0;
Test2(ref index);
Console.Write(index.ToString());
}

private void Test2(ref index)
{
index = 12;
}
 
D

daniel

Ahh, thank you Lateralus for the quick reply. I have a quick follow up
question, though:

If a reference type is always passed by reference, then (while stupid
and messy):

<code>
// with a return type this time!
void func1()
{
SomeType myType = new myType();
func2(myType);
}
void func2(SomeType myType)
{
func3(mytype);
}
void func3(SomeType myType)
{
Console.Write(myType.ToString());
}
</code>

will not cause a substantial amount of increased overhead at all (other
than variables on the stack), because only one object is every created.


Thanks,
-daniel
 
L

Lateralus [MCAD]

daniel,
That is correct. There is only one storage location of the object. The
variables all just point to the same object.
 
J

Jeff Louie

Daniel.... By default all parameters in C# are passed by value, but you
cannot
pass an actual object as a parameter. You can only a pass a reference
to an
object as a parameter. So, by default, in C# you actually pass a
reference by
value as a parameter. This is very confusing to C++ coders who assume
this
is the equivalent of C++ pass by reference. It is not. Although the
behavior is
_similar_ to C++ pass by reference, it is not identical. So C++ coders
need to
understand first that although objects use value semantics in C++,
objects in
C# use reference semantics. For instance, RefVariableA= RefVariableB
does
not call a copy constructor in C#, it simply assigns RefVariableA to
"point" to
the same object "pointed" to by RefVariableB. In C#, you pass a
reference to
an an object by value so that a copy of the reference goes on the stack.
The
object is not copied. You can touch and modify the object using the
reference
within the method, but if you reassign the reference within the method
there
are no side effects outside of the method. Of course, this does not
apply if
you override the default behaviour and explicitly pass a parameter by
reference. So to summarize, since objects in C++ use value semantics and
objects in C# use reference semantics, the concept of "pass by
reference" has
a very different meaning in C++ and C#.

Regards,
Jeff
My question: since myType is a reference type, only one instance of
this
object is ever created in memory, right? The Lead Software Architect
here (C++, no C# experience) was telling me about passing by reference--
is this implicitly done by reference types in C#? Also, could I pass by
value?<
 
G

Guest

You can't pass by value in C#, not unless the type is a value type, like
something that is declared 'struct' rather than 'class', or a basic variable
like int, byte etc.
There's no reason why you'd want to, if you did, then that'd be tantamount
to trying to do the garbage collector's work for it, and it was designed to
be global and operate off its own bat.
So just don't bother about the memory - just make it as OO as possible.
 
J

Jon Skeet [C# MVP]

Patty O'Dors said:
You can't pass by value in C#, not unless the type is a value type, like
something that is declared 'struct' rather than 'class', or a basic variable
like int, byte etc.

Not true. Everything is passed by value by default - whether the value
is a struct or a reference. You can't pass an actual *object* either by
reference or by value.
 
C

Champika Nirosh

How about assigning a deep clone of the reference type object to a new
object of same type.. at the method..

Nirosh.

Lateralus said:
daniel,
That is correct. There is only one storage location of the object. The
variables all just point to the same object.

--
Lateralus [MCAD]


daniel said:
Ahh, thank you Lateralus for the quick reply. I have a quick follow up
question, though:

If a reference type is always passed by reference, then (while stupid and
messy):

<code>
// with a return type this time!
void func1()
{
SomeType myType = new myType();
func2(myType);
}
void func2(SomeType myType)
{
func3(mytype);
}
void func3(SomeType myType)
{
Console.Write(myType.ToString());
}
</code>

will not cause a substantial amount of increased overhead at all (other
than variables on the stack), because only one object is every created.


Thanks,
-daniel
 
C

Champika Nirosh

Jon Skeet said:
Not true. Everything is passed by value by default - whether the value
is a struct or a reference. You can't pass an actual *object* either by
reference or by value.

"Everything" are u sure....?????
 
C

Champika Nirosh

Hi Jon,

I think it is not a fair comment...

"Everything is passed by value by default"

Instead.. What you would said is.. reference of the reference type variable
is passed by its' value.. or object references are passed by value by
default..

You statement is misleading... One might take it as .. object also passed by
its' value.. what happen actaully is since reference type cannot passed by
value even though you try they passes their reference..

Nirosh.
 
J

Jon Skeet [C# MVP]

Champika Nirosh said:
I think it is not a fair comment...

"Everything is passed by value by default"

It's a true comment. The only things which can be passed are value
types and references. Both of those are passed by value by default.
Instead.. What you would said is.. reference of the reference type variable
is passed by its' value.. or object references are passed by value by
default..

But that's all that *can* be passed. Note that it's not the reference
of the reference type variable - it's the *value* of a reference type
variable, and that value is a reference.
You statement is misleading... One might take it as .. object also passed by
its' value.. what happen actaully is since reference type cannot passed by
value even though you try they passes their reference..

If that's all I'd said, you'd be right. However, look at what I *did*
say:

<quote>
Not true. Everything is passed by value by default - whether the value
is a struct or a reference. You can't pass an actual *object* either by
reference or by value.
</quote>

Now, how can *that* be taken as "object also passed by its value"?
 
C

Champika Nirosh

Jon Skeet said:
It's a true comment. The only things which can be passed are value
types and references. Both of those are passed by value by default.

10: Very correct here you made the coment saying "only things which can be
passed are value
types and references. " it was not their earlier..
But that's all that *can* be passed. Note that it's not the reference
of the reference type variable - it's the *value* of a reference type
variable, and that value is a reference.

Do you see any difference, when u take the whole sentence..??
If that's all I'd said, you'd be right. However, look at what I *did*
say:

<quote>
Not true. Everything is passed by value by default - whether the value
is a struct or a reference. You can't pass an actual *object* either by
reference or by value.
</quote>

Now, how can *that* be taken as "object also passed by its value"?

Goto 10 please.....
 
J

Jon Skeet [C# MVP]

Champika Nirosh said:
10: Very correct here you made the coment saying "only things which can be
passed are value types and references. " it was not their earlier..

I think it was implied by my statement, actually (the "whether the
value is a struct or a reference" clause). Maybe it's something
that would be picked up by a native English speaker but not a
non-native English speaker though, in which case I apologise. I'm aware
that in international forums like this it helps to be specific.

However, given what I said I still believe it's impossible to take from
my paragraph the idea that an object itself can be passed by value, as
I specifically denied that.
Do you see any difference, when u take the whole sentence..??

Yes. If people start thinking that it's a reference to the variable
itself which is passed, then that gets into the territory of passing
*by* reference, IMO.
 
C

Champika Nirosh

Jon Skeet said:
I think it was implied by my statement, actually (the "whether the
value is a struct or a reference" clause). Maybe it's something
that would be picked up by a native English speaker but not a
non-native English speaker though, in which case I apologise. I'm aware
that in international forums like this it helps to be specific.

I really apprecite this...
However, given what I said I still believe it's impossible to take from
my paragraph the idea that an object itself can be passed by value, as
I specifically denied that.

Not actauly the "object also passed by its value" forgive me .. I would have
said "reference type variable also passed by its value"
Yes. If people start thinking that it's a reference to the variable
itself which is passed, then that gets into the territory of passing
*by* reference, IMO.

But only if I haven't put "passed by its' value" at the end...
 
J

Jon Skeet [C# MVP]

Champika Nirosh said:
Not actauly the "object also passed by its value" forgive me .. I would have
said "reference type variable also passed by its value"

I think it's a mistake to talk about the variable being passed by
value, partly because it makes it harder to explain what happens when
you've got an expression like "a+b" instead of a variable.

"The value that is passed is the value of the expression (which may be
a variable, a method call etc)" is perhaps clearer.

You might want to have a look at
http://www.pobox.com/~skeet/csharp/parameters.html for a more
considered approach to this - that's how I write when I've got a bit
more time, basically :)
But only if I haven't put "passed by its' value" at the end...

No, because you were talking about the "reference of the reference type
variable". It's not entirely clear what you mean there - if you were to
pass a reference *to* the variable by value, that would be very similar
to passing the variable itself (not the variable's value, but the
variable itself) by reference. Basically it's woolly terminology, and
should be avoided IMO.)
 
C

Champika Nirosh

Let me resume.. I think we both know about what we are talking... It is a
matter of how deep u explain it

Keep up the good work Jon..

Nirosh.
 

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