By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Reference Types and Value Types

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
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;
}

--
Lateralus [MCAD]
"daniel" <us**@example.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #2

P: n/a
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

Lateralus [MCAD] wrote:
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;
}

Nov 16 '05 #3

P: n/a
Daniel,
For information on reference types & value types along with passing by value
& passing by reference see:

http://www.yoda.arachsys.com/csharp/parameters.html

Hope this helps
Jay

"daniel" <us**@example.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #4

P: n/a
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" <us**@example.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
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

Lateralus [MCAD] wrote:
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;
}

Nov 16 '05 #5

P: n/a
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?<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6

P: n/a
Thank you Lateralus, Jay and Jeff.
Nov 16 '05 #7

P: n/a
<"Lateralus [MCAD]" <dnorm252_at_yahoo.com>> wrote:
To my knowledge you can't pass a "reference" type by value if you wanted
to. It is automatically passed by reference.


No. The reference is passed by value, which is quite different.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

P: n/a
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.

"daniel" wrote:
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

Nov 16 '05 #9

P: n/a
Patty O'Dors <Pa********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Nirosh.

"Lateralus [MCAD]" <dnorm252_at_yahoo.com> wrote in message
news:eL**************@TK2MSFTNGP11.phx.gbl...
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" <us**@example.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
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

Lateralus [MCAD] wrote:
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;
}


Nov 16 '05 #11

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Patty O'Dors <Pa********@discussions.microsoft.com> wrote:
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.


"Everything" are u sure....?????
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #12

P: n/a
Champika Nirosh <te**@test.lk> wrote:
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....?????


Absolutely. See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13

P: n/a
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <te**@test.lk> wrote:
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....?????


Absolutely. See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #14

P: n/a
Champika Nirosh <te**@test.lk> wrote:
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"?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #15

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <te**@test.lk> wrote:
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.


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..
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.


Do you see any difference, when u take the whole sentence..??
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"?


Goto 10 please.....
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #16

P: n/a
Champika Nirosh <te**@test.lk> wrote:
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.


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.
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #17

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <te**@test.lk> wrote:
> 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.

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.


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"
> 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.


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.


But only if I haven't put "passed by its' value" at the end...
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #18

P: n/a
Champika Nirosh <te**@test.lk> wrote:
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"


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 :)
> > 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.

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.


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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #19

P: n/a
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <te**@test.lk> wrote:
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"


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 :)
> > 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.
>
> 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.


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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.