473,386 Members | 1,715 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,386 software developers and data experts.

Object Parameters, they are passed as pointers, yes?

I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?

Thanks!

Derrick
Nov 15 '05 #1
8 1160
Correct, only value types are copied.

"Derrick" <de*********@excite.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?

Thanks!

Derrick

Nov 15 '05 #2
Consider:

class X
{
public Int32 _i;
public X(ref Int32 i)
{
_i = i;
}
}

class Test
{
static void Main(String[] args)
{
Int32 i = 5;
X x = new X(ref i);
Console.WriteLine(i);
Console.WriteLine(x._i);
i = 6;
Console.WriteLine(i);
Console.WriteLine(x._i);
}
}

What do you expect? 5 5 6 6 ?

No, it'll produce: 5 5 6 5 !

Why? Because 'i' in 'Main' will be boxed in the call 'new X(ref i)',
and, therefore, becomes a new object! Same for structs. Unless I'm wrong :)
Derrick wrote:
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?

Thanks!

Derrick


Nov 15 '05 #3
Hi Derrick,
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?


Yes, if the object is an instance of reference type (class). If the
paramter is of valuetype (struct, enum) you will have a copy of that object
for each of the classes.

But if you have parameter of type *object*
void Foo(object param)
{
}

That param might be reference to an instance of a class or boxed valuetype.
Eventhough you might share the reference to a boxed value type in the
managed heap you cannot use it for sharing data because to use the type
(read/write its properties and fields call some of its methods) you have to
unbox that valuetype and then you will have a private copy. Thus, the
changes won't go in the original.

B\rgds
100

Nov 15 '05 #4
Derrick <de*********@excite.com> wrote:
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?


Yes. However, you need to be aware of the difference between passing
parameters *by* reference and passing reference type parameters by
value.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information about this.

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

class X
{
public Int32 _i;
public X(ref Int32 i)
{
_i = i;
}
}

class Test
{
static void Main(String[] args)
{
Int32 i = 5;
X x = new X(ref i);
Console.WriteLine(i);
Console.WriteLine(x._i);
i = 6;
Console.WriteLine(i);
Console.WriteLine(x._i);
}
}

What do you expect? 5 5 6 6 ?

No, it'll produce: 5 5 6 5 !

Why? Because 'i' in 'Main' will be boxed in the call 'new X(ref i)',
and, therefore, becomes a new object! Same for structs. Unless I'm wrong :)


You're wrong. No boxing is involved in your code, and your "ref"
parameter could just as easily be a non-ref parameter. Unless the value
of the formal parameter (i in your constructor) is changed within the
method/constructor, it doesn't matter whether or not it's passed by
reference.

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

"Derrick" <de*********@excite.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I saw the ref param keyword relating to base types. Just checking, if I
pass an object around to other class constructors, there will only be one
instance of the object, correct?

Thanks!

Derrick

Nov 15 '05 #7
Damn, You're right. 'i' is copied, not boxed. Gonna have to read that
chapter again.

Jon Skeet [C# MVP] wrote:
Antenna <q1****************@hotmail.com> wrote:
Consider:

class X
{
public Int32 _i;
public X(ref Int32 i)
{
_i = i;
}
}

class Test
{
static void Main(String[] args)
{
Int32 i = 5;
X x = new X(ref i);
Console.WriteLine(i);
Console.WriteLine(x._i);
i = 6;
Console.WriteLine(i);
Console.WriteLine(x._i);
}
}

What do you expect? 5 5 6 6 ?

No, it'll produce: 5 5 6 5 !

Why? Because 'i' in 'Main' will be boxed in the call 'new X(ref i)',
and, therefore, becomes a new object! Same for structs. Unless I'm wrong :)

You're wrong. No boxing is involved in your code, and your "ref"
parameter could just as easily be a non-ref parameter. Unless the value
of the formal parameter (i in your constructor) is changed within the
method/constructor, it doesn't matter whether or not it's passed by
reference.


Nov 15 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Unless the value
of the formal parameter (i in your constructor) is changed within the
method/constructor, it doesn't matter whether or not it's passed by
reference.

Passed by reference, huh? <big grin>

Joe
--
http://www.csharp-station.com
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Miles | last post by:
Hi -- This is a bit of an implementation-specific problem, but I'd like to post it here to see if there's a general answer within the auspices of the language. I'm developing a high(er)-level...
15
by: David Fisher | last post by:
I have been encouraged by someone else to use a reference rather than a pointer as an "out" parameter to a function, eg. void doStuff(Thing &out1, Thing &out2) as opposed to: void...
9
by: Jimmy Cerra | last post by:
I am a little confused how the memory for objects is allocated in JavaScript. David Flanagan, in "JavaScript: The Definitive Guide," states that each property of a class takes up memory space when...
29
by: web1110 | last post by:
If I have 2 variables, A and B, referencing the same object and then do a A.Dispose(), what happens to B?
17
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
2
by: mati | last post by:
Suppose there is some complex functor, and we are using some helper functions inside: class Func { private: inline void helper(...) {...} public: RetType operator()(const &data, Type1 param1,...
11
by: Googy | last post by:
Hi friends!! As we know that the input parameters in a function is fixed when the function is defined but how does printf processes variable number of input arguments ? For example: 1....
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'
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.