473,883 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using ref

Hi,

Form a performance perspective, is it wise to use the ref statement as much
as possible?

Thanks!
Arjen

Jul 2 '08
65 3941
[walks away from the troll]
Jul 3 '08 #21
"What I showed is that whether
you pass a reference to an object by value or by reference in C#, in a
method/function, you can still do something 'permanent' to the object,
even when the method/function goes out of scope."
That is true, but not THE point. If your client lives, say, in Dallas, then,
when you want to reach it, you look where? in Dallas, no! if you are in
Seattle, you look at your cardfile holding your clients addresses. Now, if
one of your buddy (procedure), living in New York, ask to contact the said
client, you have two options: make a copy of your cardfile and 'pass' it to
him, that is 'by value', or you cant 'rent' him, for a while, your cardfile,
that will be 'passed by reference'. In both case, your New York buddy can
reach your client, in Dallas, and he can change your client to a not-client
anymore, quite permanently, sure, but, and there is a but, in the last case,
it can do much more, he can screw your cardfile too. And NEVER you ever
"passed" (ouch) the client it-self to your buddy.

Vanderghast, Access MVP

Jul 3 '08 #22
On Thu, 03 Jul 2008 03:33:13 -0700, Hilton <no****@nospam. comwrote:
Peter Duniho wrote:
>Hilton wrote:
What example is it that you are saying uses "ldloca.s"? Are you
comparing this to managed code? If not, how can that be "same as C"?

In any case, the "ldloca.s" instruction is used when you pass by
reference, yes. But that's only when you use the "ref" or "out"
keyword.

My point is that the C# call "method (ref x)" is defined by
you/Jon/community as being pass by reference, yet the C call "method
(&x)"
is defined by you/Jon/community as being pass by value
That point is not supported by your assertion regarding the IL generated
for some hypothetical C# code (which you didn't post anyway, so who knows
what the hell you really meant).

In any case, the passing mechanism is defined by the declaration of the
method/function, not by the caller. The caller doesn't get to pick how
the method is called. It simply complies with whatever declaration was
made.

Since there's no such thing as passing "by reference" in C, I don't see
any inconsistency at all between the two examples you gave. The fact that
in C you can generate an arbitrary reference value to pass for a parameter
is irrelevant. You're still passing the reference by value, and the
function being called still cannot change the original value that was
passed.
- they're doing EXACTLY the same thing.
They are _not_ "doing EXACTLY the same thing". The C# version is passing
the address of a variable that contains a reference to an object, while
the C version is passing the address of a variable that contains the
object itself.
Both simply take the address of x and pass it along.
Does a prettier syntax changes the entire concept?
It's not just about "prettier syntax". In fact, it's not at all about
prettier syntax. There are fundamental differences between the two
examples.
And if
Microsoft has used "&" instead of "ref" in its definition of the C#
language, would that now mean that the C# call "method (&x)" was now
pass by
value???
See above. The syntax is irrelevant. The caller is irrelevant. The only
thing that matters is the declaration of the method.
Pete, really, I don't want to spend more of your time or my time on this.
If that were true, you would stop arguing about it. Or if it's really a
true statement, then it seems that you're less concerned about my own
expenditure of time, and more concerned about how that reflects on you.
In my mind (which I agree probably doesn't fit the pure definition of the
pass-by definitions), if I pass a reference or pointer to my chunk of
data,
I'm passing by reference
You remind me of the people who continue to cling to the idea that Iraq
had something to do with the 9/11 attacks and that Hussein was hiding
weapons of mass destruction somewhere in his country, in spite of
absolutely no evidence in support of those ideas and in spite of volumes
of evidence to the contrary.

In other words, you've decided on your dogma and no amount of actual facts
will have any effect on your beliefs.

That's an unfortunate characteristic for any human being, but it's
especially problematic for someone in a field like programming. Computers
don't adjust themselves to accommodate your way of thinking. No matter
how much you wish for C to have pass by reference, and no matter how much
you wish for C#'s pass by value to be the same as C#'s pass by reference,
it simply will not be.
(the focus being on the data, not the actual
parameter). I think millions of other people think that way too since
pass-by-reference has been taught for decades when discussing C, but
apparently that capability never existed.
From K&R, section A7.3.2 "Function Calls":

In preparing for the call to a function, a copy is
made of each argument; ALL ARGUMENT-PASSING IS
STRICTLY BY VALUE

(emphasis mine)

I really don't see how it could be any clearer than that. If you continue
to insist that the simple act of passing any reference is the same as
passing "by reference", we can only conclude that you are simply guilty of
willful ignorance.

It's unfortunate that you would continue to refuse to change your view on
the matter, but the worst part is your insistence on continuing to
promulgate your view as the correct one. If you want to remain ignorant
yourself, that's fine. But please, stop trying to make everyone else
ignorant too.

Pete
Jul 3 '08 #23
Peter Duniho wrote:
It's unfortunate that you would continue to refuse to change your view on
the matter, but the worst part is your insistence on continuing to
promulgate your view as the correct one.
I have no problem discussing things with people, but I don't like it (and I
think it shows your true colors - others have mentioned this too) when you
start trying to spread BS about people. I'm not trying to 'promulgate' my
view as the correct one, look what I said in my posts:

"I know that most of the literature out there agrees with your point of
view."
"Jon, I totally 'see' you view on this (which seems to be consistent with
many/most others - i.e. I acknowledge that I am in the minority here)."

Then you accuse me of not posting code. Without trying to sound sarcastic,
I assumed someone of your knowledge and experience (I mean this, I'm not
being sarcastic) would know how to write the following code, compile and run
ildasm and see the IL:

class Class1
{
[STAThread]
static void Main(string[] args)
{
new Class1 ().Go();
}

void Go ()
{
string s = "C#";

Method1 (s);
Method2 (ref s);

}

void Method1 (string s)
{}

void Method2 (ref string s)
{}
}
Hilton
Jul 3 '08 #24
On Thu, 03 Jul 2008 11:09:23 -0700, Hilton <no****@nospam. comwrote:
I have no problem discussing things with people, but I don't like it
(and I
think it shows your true colors - others have mentioned this too) when
you
start trying to spread BS about people. I'm not trying to 'promulgate'
my
view as the correct one,
Of course you are. Every time you state your disagreement with the
correct view, you are promulgating the incorrect view.
[...]
Then you accuse me of not posting code. Without trying to sound
sarcastic,
I assumed someone of your knowledge and experience (I mean this, I'm not
being sarcastic) would know how to write the following code, compile and
run
ildasm and see the IL: [snip]
I do know how to do that. I can even write it more concisely than you
did. So what?

The point is that YOU had some code in mind, but without seeing what code
you're talking about, there's no way to understand your statement. Given
the other errors you've made, there's no way I can make the assumption
that when you write about a particular IL instruction, you actually know
what code goes with that instruction.

Especially given that the only code preceding your statement that "It uses
ldloca.s" was this: "method (StringBuilder x)", it's difficult to see why
you'd say that the IL would use "ldloca".

There certainly is code that can generate that instruction, but you hadn't
mentioned any such code up to the point where you brought up that
instruction.

If I were going to give you the benefit of the doubt and assume that you
actually knew what you were talking about, I'd just ignore this whole
thread, figuring that when you write that passing an object's reference is
the same as passing the parameter "by reference", what you really mean is
that it's actually the same as passing the parameter "by value".

It's pretty obvious that making that assumption would be incorrect, and so
it's also pretty obvious that I can't make any other assumptions about
anything else you post. If you are going to state that some specific code
generates some specific IL instruction, you had darn well better post the
code that you claim generates that IL instruction. Otherwise, whatever
statement you make about the IL instruction doesn't add a single thing to
the discussion.

Pete
Jul 3 '08 #25
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
The fact that the value you're passing is a reference does not make the
parameter a "by reference" parameter, nor does it mean that the object is
passed by reference.

IMHO, Jon's article does in fact explain this.

Pete
Yes it does, and some days I'm slower than others. It didn't sink in for me
until Jon put up the Swap example.

- Steve
Jul 3 '08 #26
On Jul 3, 11:06*am, "Steve Harclerode" <Lizard.That... .@hot.mail.com>
wrote:
>
Yes it does, and some days I'm slower than others. It didn't sink in for me
until Jon put up the Swap example.
Then you'll love my example even more. And Jon's Swap example had a
typo in it (that I corrected).

Remember the rules of Ray then: for optimal performance, use Ref.
For stuff that's primitive, use non-Ref (Swap by value). For objects
(including arrays, strings [all forms, including String,
StringBuilder], user defined classes), use ref only if "new" is
present in the function/method, or only if you are 'redirecting' the
object like with "Swap", otherwise it's OK to use pass-by-value.

RL

Jul 3 '08 #27
On Jul 3, 11:09*am, "Hilton" <nos...@nospam. comwrote:

Hilton--FYI Peter is a bit dogmatic, been that way for years, but he
is a useful reference here. We see your point, but I say give it a
rest. But for Jon and Peter, we wouldn't have decent programmers
working for free.

RL

[C# "newbie" MVP]

Jul 3 '08 #28
On Thu, 03 Jul 2008 11:55:13 -0700, raylopez99 <ra********@yah oo.com>
wrote:
[...]
Remember the rules of Ray then: for optimal performance, use Ref.
For stuff that's primitive, use non-Ref (Swap by value). For objects
(including arrays, strings [all forms, including String,
StringBuilder], user defined classes), use ref only if "new" is
present in the function/method, or only if you are 'redirecting' the
object like with "Swap", otherwise it's OK to use pass-by-value.
And to Steve: I _strongly_ recommend you use Google Groups to review
previous discussions and posts by Ray. You should take everything you
read on the Internet with a grain of salt anyway, but in this case you can
make a decision for yourself as to what advice is likely to be correct,
useful, etc. by examining previous contributions and seeing if they look
like they were written by someone you'd consider reliable.

And for what it's worth, the same thing is true for any of the rest of
us. You shouldn't accept any of our statements without at least some
critical consideration, and I'm not going to try to convince you that Jon,
I, or anyone else is more or less reliable than someone else. You can
make an informed decision yourself just by looking at past contributions
to the newsgroup.

As far as this particular question goes, it's my strongly-held opinion
that using "ref" as a performance tool is almost never going to be useful
in a real-world scenario. If it does help, then it's because there's
something more fundamentally wrong about your design or implementation.

'nuff said. :)

Pete
Jul 3 '08 #29
On Jul 3, 8:42*am, "Michel Walsh"
<vanderghastAro baseMsnDot...@n ospam.comwrote:
"What I showed is that whether
you pass a reference to an object by value or by reference in C#, in a
method/function, you can still do something 'permanent' to the object,
even when the method/function goes out of scope."

That is true, but not THE point.
Thanks Michel Walsh! "That is true..."; I take that as a sort of
newbie moral victory of sorts, kinda, sorta.
If your client lives, say, in Dallas, then,
when you want to reach it, you look where? in Dallas, no! if you are in
Seattle, you look at your cardfile holding your clients addresses. Now, if
one of your buddy (procedure), living in New York, ask to contact the said
client, you have two options: make a copy of your cardfile and 'pass' it to
him, that is 'by value', or you cant 'rent' him, for a while, your cardfile,
that will be 'passed by reference'. In both case, your New York buddy can
reach your client, in Dallas, and he can change your client to a not-client
anymore, quite permanently, sure, but, and there is a but, in the last case,
it can do much more, he can screw your cardfile too. And NEVER you *ever
"passed" (ouch) the client it-self to your buddy.
So are you saying 'pass-by-value' is like 'constant' in C++ (which to
my newbie knowledge C# lacks) in that you can safely pass an object to
a function so it cannot be manipulated to change the original? If so,
please let me know how to do that, since honestly once I learned C# I
have yet to find how you can make a function/method take a 'read only'
copy of a class (not a primitive type, but of a class). In fact,
Googling this confirmed this fact (see below). So I don't see how
pass-by-value is any 'safer' (or read-only) than 'pass-by-value' for a
class passed.

RL

Googling on 'const', note the **** text ****--RL

The const keyword only applies to variables, which MSDN calls
“fields”. A field with the const keyword indicates that the field
cannot be changed. The value of the field must be initialized in the
declaration. Interestingly, attributes cannot be declared as const,
and furthermore, attributes referring to const fields can only have
getters, not setters.

****In C++, the const keyword was also used to indicate that the
method did not modify any variables:****
void Foo() const;

****or that the method returned a reference that could not be
modified:****
const int& Foo();

or that the parameter being passed as a reference could not be
modified by the method:
void Foo(const int& i);

which could lead to some ridiculous but valid statements:
const int& Foo(const int& i) const;

Happily, C# treats the keyword const with only one meaning.

Declaring fields as const protects both you and other programmers from
accidentally changing the value of the field. Also note that with
const fields, the compiler performs some optimization by not declaring
any stack space for the field.

Furthermore, const can only be applied to intrinsic types. You can’t
say:
const HasConstField hcf2=new HasConstField() ;
Jul 3 '08 #30

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

Similar topics

5
5730
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ================================================================= Error 19: "CORBAManagerMessages.h", line 4 # Unexpected 'std'. using std::string; ^^^
3
2169
by: Mike L | last post by:
Should the command call "using" be before or after my namespace? **AFTER** namespace DataGridBrowser { using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections;
3
2445
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use project classes with 'using' meaning
14
5809
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do tell those to me too coz I have no idea how to go about doing it.
8
2418
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL into the bin directory but am not able to progress. Can anyone please guide me to an online tutorial on the subject. Thanks,
0
2210
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase .NET 2.0 DataProvider (iAnywhere.Data.AsaClient.dll) into the GAC so it can be used in the ProviderName property of a SQLDataSource and loads properly at run time. The application I'm writing is a bit more complex than the example I'm about to...
10
1981
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL, CN) Dim DR As OleDbDataReader = CMD.ExecuteReader()
0
2584
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation link (1,2,3 so on). The code can be found in SubscriptionCart.aspx.cs. Default.aspx ------------
3
8315
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and then decrypting, the resulting decrypted file under .NET 2.0 is garbage, the .NET 1.1 build works...
6
5180
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick application working. However, when attempting to implement the solution, the AJAX calls weren't updating the screen like the examples were and seemed not to fire until after the long running process had completed. I found the only real...
0
9944
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
9796
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
11153
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...
1
10860
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
9583
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
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3239
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.