473,406 Members | 2,259 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,406 software developers and data experts.

c# passing objects as method parameters??

Hi

Could someone clarify for me the method parameter passing
concept?

As I understand it, if you pass a variable without
the "ref" syntax then it gets passed as a copy.

If you pass a variable with the "ref" syntax then it gets
passed as a reference to the object and any changes to
the variable will be reflected in the calling method.

What happens with objects??? For example if I pass a
SqlConnection object from one method to another do I need
to ensure I use the "ref" syntax or are objects dealt
with differently to variables???

Thanks in advance

Andy
Jul 21 '05 #1
5 36366
For objects, a copy of the *reference* gets passed - not a copy of the
object.

Meaning, that the copy of the references you have, can be changed, without
modifying the original one.

So:

public void MyFunc(SqlConnection conn)

{
conn = null;
}

Sets the copy to null. However, the original SqlConnection reference that
you passed in here is still pointing to what it was. However:

public void MyFunc(ref SqlConnection conn)

{
conn = null;
}

There is no copy passed in - the actual reference is passed. Thus setting it
to null, will set the original one to null as well (they are one and the
same).


"Andy" <an**@thecodeclinic.net> wrote in message
news:3e****************************@phx.gbl...
Hi

Could someone clarify for me the method parameter passing
concept?

As I understand it, if you pass a variable without
the "ref" syntax then it gets passed as a copy.

If you pass a variable with the "ref" syntax then it gets
passed as a reference to the object and any changes to
the variable will be reflected in the calling method.

What happens with objects??? For example if I pass a
SqlConnection object from one method to another do I need
to ensure I use the "ref" syntax or are objects dealt
with differently to variables???

Thanks in advance

Andy

Jul 21 '05 #2
Andy <an**@thecodeclinic.net> wrote:
Could someone clarify for me the method parameter passing
concept?


See http://www.pobox.com/~skeet/csharp/parameters.html - let me know if
you still have any questions after that.

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

You do not NEED to use the ref keyword when passing objects as parameters,
and under most circumstances you won't. When you pass an object as a
parameter a copy of the reference is passed to the method so you can still
access the properties and methods of the object.

Now, let's examine the case where the method changes the object that the
parameter references (e.g. the method creates a new SqlConnection and has
the parameter reference it). If you do not use "ref" the method will use the
newly created object but the variable that the caller passed in to the
parameter will remain unchanged and continue to reference the original
object. If you do use "ref" then the method will use the newly created
object AND the variable that the caller passed in to the parameter will also
reference this newly created object.

--
Rob Windsor
G6 Consulting
Toronto, Canada

"Andy" <an**@thecodeclinic.net> wrote in message
news:3e****************************@phx.gbl...
Hi

Could someone clarify for me the method parameter passing
concept?

As I understand it, if you pass a variable without
the "ref" syntax then it gets passed as a copy.

If you pass a variable with the "ref" syntax then it gets
passed as a reference to the object and any changes to
the variable will be reflected in the calling method.

What happens with objects??? For example if I pass a
SqlConnection object from one method to another do I need
to ensure I use the "ref" syntax or are objects dealt
with differently to variables???

Thanks in advance

Andy

Jul 21 '05 #4
Andy wrote:
Hi

Could someone clarify for me the method parameter passing
concept?

As I understand it, if you pass a variable without
the "ref" syntax then it gets passed as a copy.

If you pass a variable with the "ref" syntax then it gets
passed as a reference to the object and any changes to
the variable will be reflected in the calling method.

What happens with objects??? For example if I pass a
SqlConnection object from one method to another do I need
to ensure I use the "ref" syntax or are objects dealt
with differently to variables???

Thanks in advance

Andy


*All* object parameters are passed by reference. By default all value type
(struct, integers, real numbers, enums) are passed by value. When a
parameter is passed by value a copy of the type is put on the stack and the
method has access to the copy, not the original. This means that if you
change the parameter passed by value in the method, the original is not
affected.

void f(int i)
{
i = 4; // no effect on original item
}

int j = 10;
f(j); // j is not affected

When a parameter is passed by reference (all object parameters, and 'ref'
for a value type) a reference is passed, in effect this is like a pointer
pointing to the actual item. In C# the passed-by-reference item is accessed
just like a pass-by-value item, which I think is a bad thing, in managed C++
a value type passed by reference is passed as a managed pointer and hence it
is clearer what is happening. If the method changes a value in the item
(for example, a field in a struct passed by reference, or a member of an
object) then the original item is changed.

void f(ref int i)
{
i = 4; // original item is changed
}

int j = 10;
f(ref j); // j now has a value of 4

class T { public int x = 10; }
void g(T t)
{
t.x = 42; // original is affected
}

T o = new T(); // o.x is 10
g(o); // o.x is now 42

However, if you change the actual parameter value to refer to another
object, the original is unaffected because you are merely changing the stack
frame, which no longer exists when the method returns. Using the T class:

// t is a copy of the reference to the object on the stack frame
void h(T t)
{
// change the reference to another object
t = new T();
} // stack frame destroyed, so new reference is destroyed too

T o = new T();
o.x = 99;
h(o); // o is unaffected

If you want to affect the reference to an object in the original stack frame
you need to use the ref keyword. Instead of a copy to the reference to the
object (which is the case without ref) you know have a reference to a
reference to the object (in C++ a pointer to a pointer to an object), so if
you change the item referred to by the parameter the original reference to
the object is changed. In C~ you don't see this de-referencing, but you have
to explicitly do this in managed C++.

For example:

class U
{
public string s;
public U(string str) {s = str;}
}

void q(ref U u)
{
Console.WriteLine("passed " + u.s);
u = new U("second"); // replace the original reference with a reference to
this new object
}

U u = new U("first"); // create an object, and pass a reference to q()
Console.WriteLine("created as " + u.s);
q(ref u); // when this returns u is the object created in q()
Console.WriteLine("changed to " + u.s);

Here, you can pass an object to q() and q() will change that object to
another one. You are more likely to want to have an object returned through
the parameter, without passing a reference into the method. To do this use
'out' rather than 'ref'

void p(out U u)
{
u = new U("second"); // replace the original reference with a reference to
this new object
}

U u; // not initialized
p(out u); // when this returns u is the object created in p()
Console.WriteLine("changed to " + u.s);

Richard
--
my email ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)
Jul 21 '05 #5
<"Richard Grimes [MVP]" <read my sig>> wrote:
*All* object parameters are passed by reference.


No, they're not. Objects aren't passed at all. References (the values
of reference-type expressions) are passed by value.

References being passed by value is *not* the same as objects being
passed by reference. I view terminology like this as very important to
get right.

See http://www.pobox.com/~skeet/csharp/parameters.html for a more
detailed explanation.

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

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

Similar topics

5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
6
by: Bryan Martin | last post by:
I have a object that is created in a seperate domain which needs to be passed back to the parent class. Because this object is created in a seperate domain if I try to pass the object back to the...
5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
3
by: Tor Inge Rislaa | last post by:
Passing and using parameters How to get hold of the parameter value provided by the URL? When I open a web form with a DataSource I often include a parameter in the URL as below: ...
4
by: Silent1Mezzo | last post by:
Declaration: Item foodItems = new Item; Call: this.writeFile(foodItems); Function private void writeFile(Item items) { items.ToString();
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: 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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.