473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Reference Types and Value Types

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
19 2116
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
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
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
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
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
Thank you Lateralus, Jay and Jeff.
Nov 16 '05 #7
<"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
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
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
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

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

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
9
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
1
by: az.anonymous | last post by:
Im starting to learn C#, and I made a simple stack class. Inside the stack class I had the following: class StackElement { object info; StackElement below; } Everything works fine cause...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.