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

Passing Objects by ref keyword as a convention


Hello fellow C# programmers,

This question is more about general practice and convention so here
goes:

I got into a discussion with a co-worker who insisted that as a general
practice all objects should be passed by reference using the ref
keyword generally speaking because as the writer of code you are
conveying your intentions that an Object should/can be modified by your
function.

For example: public static void ChangePropertyOfObject( ref Obj o )

I actually think that it's wrong to use the 'ref' keyword all the time
because you then allow someone to modify the reference of the object.
So they can potentially create a new object or set the current object
to null.

To me using the ref keyword in everyday code shouldn't be the case and
I don't exactly agree with him. But after a long discussion I'm
actually getting a little unsure of myself.

So what do you guys think? I recognize there are some instances when
using 'ref' keyword may have some benefits but to my knowledge most of
my code that I write doesn't use it and I think it's bad practice to
use it all the time.

Anybody care to help me gain my confidence or am I just flat out the
one who is wrong here?

Thanks guys & gals and happy programming.

-R

Jan 9 '07 #1
4 2797
Deckarep wrote:
This question is more about general practice and convention so here
goes:

I got into a discussion with a co-worker who insisted that as a general
practice all objects should be passed by reference using the ref
keyword generally speaking because as the writer of code you are
conveying your intentions that an Object should/can be modified by your
function.
I actually think that it's wrong to use the 'ref' keyword all the time
because you then allow someone to modify the reference of the object.
So they can potentially create a new object or set the current object
to null.

To me using the ref keyword in everyday code shouldn't be the case and
I don't exactly agree with him. But after a long discussion I'm
actually getting a little unsure of myself.
He has misunderstood the ref keyword.

You can modify an object with or without the ref keyword.

The ref keyword allows you to replace the object.

Two completely different things.

And it is certainly not best practice to use the ref keyword
all the time.

I consider it very rare to need it.

Arne
Jan 9 '07 #2
hi,
I got into a discussion with a co-worker who insisted that as a general
practice all objects should be passed by reference using the ref
keyword generally speaking because as the writer of code you are
conveying your intentions that an Object should/can be modified by your
function.
No - in fact you would be explicitly requiring that the user of your method
_must_ [*1] supply such parameters with the ref modifier included (at the
callsite), thereby allowing your method to modify such reference such that
after the call the calling routine may now have references to entirely
different object(s) than before said call.

Why on earth would you want to go to all that trouble if your didn't in fact
have any requirement to modify the reference itself, as opposed to
manipulating the object it points to.

my $A0.0348,

gary

--
http://www.oxide.net.au

*[1] - Optionality in using 'ref' at the callsite can be provided by
providing an override method without the 'ref' modifier, which simply calls
the original method with the 'ref' modifier supplied. Combinatorial
explosions ensue should there be multiple such parameters.



Jan 9 '07 #3
Deckarep wrote:
I got into a discussion with a co-worker who insisted that as a general
practice all objects should be passed by reference using the ref
keyword generally speaking because as the writer of code you are
conveying your intentions that an Object should/can be modified by your
function.
Your coworker is flat-out wrong. Any class with a writable property
(or a public field) is conveying the intent that users may change its
state. To a lesser degree, public instance methods convey this, too.
If you don't want users to change an object's state, don't provide
property set methods.
For example: public static void ChangePropertyOfObject( ref Obj o )
Fwiw, this makes for bigger, slower code than code that simply passes
(Obj o) by value: all calling code has to include the "ref" keyword,
and what is passed is actually a pointer to a reference, not the
reference itself. This means that every reference to the parameter
within ChangePropertyOfObject is a little bigger and a little slower
than it needs to be.

More importantly, by making every call use the "ref" keyword, you lose
the specialness of passing "ref" parameters. It no longer stands out
when a method call may change the passed parameter.
I actually think that it's wrong to use the 'ref' keyword all the time
because you then allow someone to modify the reference of the object.
So they can potentially create a new object or set the current object
to null.
Exactly. The point of "ref" parameters is not that the call may change
the state of the passed parameter; the point of "ref" parameters is
that the call may change the instance that the caller is pointing to.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
Jan 9 '07 #4

Deckarep wrote:
Hello fellow C# programmers,

This question is more about general practice and convention so here
goes:

I got into a discussion with a co-worker who insisted that as a general
practice all objects should be passed by reference using the ref
keyword generally speaking because as the writer of code you are
conveying your intentions that an Object should/can be modified by your
function.
Your coworker misunderstands the semantics of parameter passing in
..NET. He may understand the technical details of what "ref" does, but
he doesn't understand the rest of the parameter passing picture enough
to put "ref" in its proper context.

Simply passing an argument of a reference type, Object in your example,
indicates that the caller can change the object's state. That's what
passing a reference by value means: you can change the object's state,
but not replace the object with another object (or with null).

Therefore, by using "ref" all the time, you are _obscuring_ your
intentions: you're pretending that every method may choose to replace
its parameter objects with other objects when in fact that isn't true.

I'm guessing that your coworker's objection to non-"ref" parameter
passing is that simply saying

Object o = ... ;
Foo(o);

doesn't adequately convey that the state of o can be changed, perhaps
as opposed to this:

int i = 3;
Foo(i);

....but if that is his objection then all that that tells me is that he
isn't fully versed in parameter passing conventions in .NET and needs
to read Jon Skeet's article:

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

and maybe some others as well.

If my arguments don't convince, then consider the following: the .NET
Framework itself does not use "ref" everywhere. In fact, it uses it
very, very rarely. These are the same people who designed C# and .NET,
and _they're_ not doing this. Therefore, your coworker is claiming that
the Framework designers don't understand parameter passing as well as
he does. The .NET design team isn't _always_ right, but proving that
they're off base in such a huge way is more than anyone I know can pull
off.

Jan 9 '07 #5

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
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...
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...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
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);
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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.