473,756 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ChangePropertyO fObject( 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 2812
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 ChangePropertyO fObject( 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 ChangePropertyO fObject 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
14946
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
5
36412
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 variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
13
17929
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 create another Area and assign myArea to it (ie: Area foo = myArea;) or is there a better way? ~AF
8
2118
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, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
17
9391
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 commands on the same connection. I have an understanding that if SqlConnection is passed as "value" (unboxed), object B will create its own copy of SqlConnection, so when object A closes its connection, it remains open for object B's copy. Is this
6
2845
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 types hold a reference to an object as opposed to object data itself. So, when reference type parameter is passed into a method, a copy of objects reference is passed in, so called method can do whatever to "original" object and a caller will see...
12
2684
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
3306
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
14
6023
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.