473,729 Members | 2,348 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 3907
On Thu, 03 Jul 2008 00:36:38 -0700, Hilton <no****@nospam. comwrote:
Jon,

I have to disagree with you that objects themselves are passed by value.
Jon never said they were. In fact, he pointedly stated that objects are
themselves never passed at all in C#.

I agree with this viewpoint, and it's consistent with the usage of the
terms in the C# specification, as well as MSDN's documentation of C#.
Passing by value means that you pass the value; i.e. a byte[] (or
Vector, or
Hashtable) being passed as value passes the entire thing - that doesn't
happen.
Straw man. You're arguing a point that was never in contention.
Also, passing by value means that the thing cannot be changed;
That's not what "passing by value" means. It's true that the original
argument cannot be changed, but that's just a side-effect of what "by
value" really is: making a copy of the original argument and passing that
to the method.
hence passing by value not reference. But when you do "method (list)",
method can change list because a reference to the list is being passed,
not
the list (aka value).
The method can _not_ change the original argument that was passed, which
is what distinguishes passing "by value" and "by reference".
C# simple passes a reference to the object which is
the same as C passing in a pointer to the data.
Not really. In C, you have the option of passing the data as a complete
copy. This simply doesn't exist in C#. The reference _is_ the data. But
more importantly, C++ distinguishes between passing a pointer by value,
and passing a pointer by reference. It's true that before C++ came along
and introduced the passing of parameters by reference, people would just
pass a pointer and call that "by reference". But it's not really what was
happening. In C/C++ when you pass a pointer without the "&" as part of
the parameter declaration, you are passing a pointer _by value_.
And the method cannot
change the reference in C# just as the method in C cannot change the
pointer
(from the caller's point of view) - same thing.
That's right. Because in both cases the parameter is passed by value, the
original storage for that value cannot be modified by the method/function
being called.
Pass by reference:
C# - passes a reference to the data (not the data) and the data can be
changed by the method
Wrong. The fact that a reference is being passed does not mean the
parameter is being passed "by reference".
C - passes a pointer to the data (not the data) and the data can be
changed
by the method
Again, wrong. The pointer is being passed by value.
The example you give on your page "method (StringBuilder x)" then "x =
null"
is exactly the same as C having "method (int *x)", then "x = NULL"; i.e.
pass by reference.
Wrong. Both are passing by value. That's why the caller's copy of the
value is not changed when the method assigns "x" to "null".
If you view "x" as a reference to the StringBuilder
object, then you're passing the object by reference
Saying the same thing over and over again isn't going to make it true.
The fact that a reference is being passed does not make it "passing by
reference". The reference is passed by value.
(which you said did not
happen). If you view "x" as the actual StringBuilder object,
Why would we do that? The actual object is accessible only through a
reference. There is no way to put the object itself into a variable.
then the VM is
effectively passing &x (C terminology) - again, by reference. It uses
ldloca.s which is defined as "Loads the address of the local variable at
a
specific index onto the evaluation stack, short form." - same as C.
"It uses"? What uses? You haven't posted any code that you could be
referring to. 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.
Try it and see. If you just pass an object reference by value, a plain
"ldloc" instruction is used, not "ldloca".

If this is the basis of your argument, then I'd say you just blew up your
own argument. By your own description, since "ldloca" is _not_ used when
you pass by value, even when the argument is a object reference, obviously
the object reference is being passed by value, not by reference.
Apart from ints, floats, etc, C# passes all objects by reference.
No. C# passes _everything_ by value, unless you state otherwise with
"ref" or "out". The fact that the value is sometimes a reference is
immaterial. It is still passed by value.
"ref"
simply passes the address of the reference thereby allowing the method to
change it (exactly the same as C).
That's right...it passes a _reference_ to the reference. That is, using
"ref" is how you pass "by reference". If you're not using "ref" or "out",
you're not passing by reference.
"out" is the same as "ref" except that
the method is forced, by the compiler, to assign a value to the
parameter.
And the caller is not.
How is this C code that uses pass by reference:

byte* b = {some array of bytes};
method (b);
That's not C code that uses pass by reference. It's passing a pointer by
value.
...different to this C# code?

byte[] b = new byte[]{some array of bytes};
method (b);
It's exactly the same and both are passing by value.
In a meeting, would you ever say "Add the two string references together
to
get the filename"? I wouldn't, I'd say "Add the two strings together to
get
the filename".
I would never say "add the two strings" or "add the two string
references". I would say "concatenat e the two strings". But how is this
at all related to the question of passing by value or by reference?
So, if we view "s" as a string, then "method (s)" is pass by
reference.
Huh? If we view "s" as a string, then "method(s)" is passing the object
"s" by value. Even using your incorrect terminology, the "method(s)" can
only be "pass by reference" is we view "s" as a reference.
Jon, Peter, before you reply, I know that most of the literature out
there
agrees with your point of view, so no need to rehash what they have
already
written.
I beg to differ. As long as you insist on posting a claim contrary to the
truth, there will be a need to "rehash what they ahve already written".

All that literature out there that agrees with our point of view, it
didn't just happen by accident. There's a _reason_ that the vast majority
of C# documentation (and _all_ of the definitive documentation, such as
the specification itself) agrees with this point of view.
Maybe it's just me, but I cannot see how C# passes objects by
value.
It'd be one thing if you were approaching this naïvely. We could just
explain how things really work, and you could get on with whatever it is
you're doing. But you've obviously put a lot of effort and thought into
your argument. Which means you're simply not paying attention to reality.

Don't get confused by the fact that the same word is used in two different
ways. That's a huge mistake on your part.

And consider this: if you are already always passing objects by reference,
then what does it mean to use "ref" when you are passing an object? Are
you passing "by reference by reference"?

Pete
Jul 3 '08 #11
Jon Skeet wrote:
Hilton wrote:
>I have to disagree with you that objects themselves are passed by value.
>Where did I say that they were? I said that *references* are passed by
value. Objects aren't passed at all.
You replied to Steve:

Steve Harclerode <Li************ *@hot.mail.comw rote:
The objects themselves are passed by reference anyway
No, they're not.
---

I then made the assumption that if the object wasn't passed by reference, it
is passed by value - which is it?

[zap your reply]

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). My
point is that a method works on data and I care about the data, I care about
the stuff that is in arrays, lists etc. Now, even though you assert that "C
doesn't have pass by reference at all.", for decades millions of people were
taught the difference between passing by reference and passing by value
using C, were they wrong? If I have data, and I don't pass that data to a
method, but rather pass a reference/pointer (whatever the syntax), then (in
my mind) it is pass by reference. C# and C really aren't that different in
these terms, just that it is hidden (thankfully) - I don't think that the
syntax of the language should change the terminology of what really is
happening under the covers. The key here is that you're thinking strictly
in terms of the parameters, I'm thinking in terms of the data.

Wow, I'm still trying to wrap my head around your "C doesn't have pass by
reference at all" comment. I understand your logic of it, but it sure as
heck doesn't make any sense to me. Having said that, I think Wikipedia sums
up our two opposing views in this line: "Java is a call-by-value language,
but since most Java expressions are references to anonymous objects, it
frequently displays call-by-reference semantics without the need for any
explicit reference syntax."

Hilton
Hilton
Jul 3 '08 #12
On Jul 3, 11:09*am, "Hilton" <nos...@nospam. comwrote:
Jon Skeet wrote:
Hilton wrote:
I have to disagree with you that objects themselves are passed by value.
Where did I say that they were? I said that *references* are passed by
value. Objects aren't passed at all.

You replied to Steve:

Steve Harclerode <Lizard.That... .@hot.mail.comw rote:
The objects themselves are passed by reference anyway

No, they're not.
---

I then made the assumption that if the object wasn't passed by reference,it
is passed by value - which is it?
Well, you could have read the *very next sentence* in my reply, which
explained what actually happens:

"The references are passed by value."

Your assumption reminds me of the "When did you stop beating your
wife?" question.
[zap your reply]

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). *My
point is that a method works on data and I care about the data, I care about
the stuff that is in arrays, lists etc. *Now, even though you assert that "C
doesn't have pass by reference at all.", for decades millions of people were
taught the difference between passing by reference and passing by value
using C, were they wrong?
Yes. If they'd read K&R, I believe they'd have seen something
explicitly saying that C only supports pass by value. (I don't have a
copy to hand, but I'm pretty sure I've seen it there before.) You can
*simulate* pass by reference in C, but it's not true pass by
reference.
*If I have data, and I don't pass that data to a
method, but rather pass a reference/pointer (whatever the syntax), then (in
my mind) it is pass by reference.
Then your mind is wrong by the technical definitions of pass by
reference.
See http://en.wikipedia.org/wiki/Evaluation_strategy
>*C# and C really aren't that different in
these terms, just that it is hidden (thankfully) - I don't think that the
syntax of the language should change the terminology of what really is
happening under the covers.
Whether the language supports pass by reference or not is defined by
the language specification. C# *does* support pass by reference using
the "ref" keyword. C doesn't support it (directly - you can emulate it
but that's not the same thing).
>*The key here is that you're thinking strictly
in terms of the parameters, I'm thinking in terms of the data.
I'm thinking in terms of the meanings of technical terms, which are
how we communicate.
Wow, I'm still trying to wrap my head around your "C doesn't have pass by
reference at all" comment. *I understand your logic of it, but it sure as
heck doesn't make any sense to me.
That's because I believe you've been misunderstandin g the meaning of
"pass by reference" as indeed many people do.
Pass by reference has a very specific meaning, and it isn't directly
supported by the C language. You have to work round the lack of
support by explicitly passing (and receiving) a pointer *by value*.

It's not just me that says that C doesn't directly have pass-by-
reference semantics though:
http://en.wikipedia.org/wiki/C_(programming_language)
http://www.math.cam.ac.uk/undergrad/...al/node50.html
http://www.comp.nus.edu.sg/~esim/faq/node12.html

As I say, I believe it's explicitly stated in K&R as well.
>*Having said that, I think Wikipedia sums
up our two opposing views in this line: "Java is a call-by-value language,
but since most Java expressions are references to anonymous objects, it
frequently displays call-by-reference semantics without the need for any
explicit reference syntax."
Yes, the effects can be similar in many cases. That doesn't mean it's
the same thing. Calling C#'s default behaviour "pass by reference" can
be very confusing (I've seen several people get confused by it) and
often leads to the mistaken belief that adding the "ref" modifier for
a parameter which uses a reference type makes no difference. After
all, if it's already being passed by reference, what difference could
"ref" make?

Once you understand that the value of a variable (or any expression)
is either a value type value or a reference (including null) you end
up with a much more consistent mental model which works well with
parameters, assignment, GC etc. Trying to think of the world in terms
where the value of a variable is the object itself falls down all over
the place, as well as being further removed from the technical
details. Why do you want to push a more complicated world view which
just doesn't stand up to scrutiny?

Jon
Jul 3 '08 #13
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 - they're doing
EXACTLY the same thing. Both simply take the address of x and pass it
along. Does a prettier syntax changes the entire concept? 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???

Pete, really, I don't want to spend more of your time or my time on this.
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 (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.

Hilton
Jul 3 '08 #14
if I pass a reference or pointer to my chunk of data,
I'm passing by reference
You can keep repeating this, but it doesn't make it true. And re-
iterating it only adds confusion for new C# developers; one of the
reasons so many people get it wrong is because others (like yourself)
seem to be going out of their way to keep the myth alive.

In short: no; you simply happen to be passing a value that *is* a
reference. Bass-by-reference (of a class) would be passing a reference/
pointer to something that *itself* happens to be a reference. In this
case, it is the address of the variable/field "x" that is actually
passed. In the original case, it is the current *value* of the
variable/field "x" that is passed - i.e. the reference to the
instance. The explanation works better with a picture, but...

I very-much doubt my voice will sway you much, but there we go...

Marc
Jul 3 '08 #15
On Jul 3, 11:33*am, "Hilton" <nos...@nospam. comwrote:
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 - they're doing
EXACTLY the same thing.*Both simply take the address of x and pass it
along. *Does a prettier syntax changes the entire concept? * 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???
You're only looking at the calling side. Look at the declaring side.

In C, you're declaring the parameter with a different type - a pointer
type. You then have to *use* that parameter differently as well,
explicitly dereferencing etc.
In C#, you declare that the parameter is passed by reference, but the
actual parameter has the same original type, and can be used within
the method as if it were any other variable of that type.

If Microsoft had used & but still had all the pass by reference
semantics on the declaring side, it would still have been pass by
reference. If it had involved changing the parameter type to be a
pointer, and then dereferencing the pointer etc, it would have been
pass by value.
Pete, really, I don't want to spend more of your time or my time on this.
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 (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.
The ability to simulate it has existed, but the calling mechanism has
always been pass-by-value.

Look at it this way:
1) You accept that you're technical incorrect
2) Your way of thinking *has* confused people in the past, several
times
3) Your mental model makes talking about other concepts such as
assignment harder
4) Your mental model leads to confusion when reference types *are*
passed by reference

Where exactly are the benefits of using the inaccurate terminology?

Since writing the article on my web page, I've had dozens of people
emailing me to say they now understand both parameter passing *and
other aspects of reference types* much clearer. Why try to cling onto
an inaccurate model where the value of a variable is an object instead
of a reference?

Jon
Jul 3 '08 #16
On Jul 2, 10:46*pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
So consider this code:

public void Swap (object a, object b)
{
* * object tmp = a;
* * a = b;
* * b = a;

}

If the parameter were passed by reference by default, that would work.
As it is, it does nothng.
This example is totally wrong Jon. You probably meant to write
"b=tmp;"
And you call yourself a "[C# MVP]"? Well, I can call myself an "C#
N00b MVP" then.

But we all make mistakes--in my previous post in this thread, I stated
'ref' is only needed when objects are passed if the calling method
uses 'new'--this is not strictly speaking true, as evidenced by the
line by Jon above "object tmp = a;". (Though, to be pedantic, "object
tmp = new object(); tmp = a;" would also work, but not be as concise
and fast).

To demonstrate that for most stuff, except for swap, 'new' and stuff
where you are redirecting the references, I wrote a short program
below to show when to use 'ref' and pass-by-reference for reference-
types (objects, strings) and when to not use 'ref' and just rely on
pass-by-value for reference-types, which usually for most stuff gives
the same result.

As you can see for the string (a reference type) passing by reference
is the same as passing by value when changing the string--and outside
the method the string *is* changed. However, if 'new' were involved,
it would be different.

You can see this by looking at the StringBuilder methods below--one is
pass-by-value, and the other is pass-by-reference. Both 'do stuff' to
the strings that are persistent--even outside the methods--for example
X.Append("SB1") changes the string in the pass-by-value method. But
when 'new' is involved, you must pass-by-reference when passing a
reference-type parameter (such as a string, an array, an object).

In the second example, the traditional "swap" method is demonstrated,
which shows again why 'ref' keyword is needed to make such a
'redirection' of objects and/or references to objects persistent
outside the method.

Ray Lopez
[C# N00b MVP]
-
using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ParaPassingPass ByValByRef_01
{
class Program
{
static void Main(string[] args)
{
StringBuilder mainY = new StringBuilder() ;
mainY.Append("H ellow");
Console.Write(" Output of mainY is true or false?...");
Console.WriteLi ne(mainY != null);
Console.WriteLi ne("value of string mainY is:{0}", mainY);
// mainY = null; //causes runtime error!
mainY.Append(" ");

// ignore the next few lines dealing with mainY2--it's an
attempt to see if a string can be set to null

StringBuilder mainY2 = new StringBuilder ("");
mainY2 = null;

Console.Write(" ............... ...");

//mainY2.Append(" hello2");
//Console.WriteLi ne("str hello2: {0}", mainY2);

Console.WriteLi ne(mainY2 == null); //it's dangerous to do
anything but check for this condition when setting an object to
'null' (runtime error)
// end of mainY2

FooClass1 myFC1 = new FooClass1();
myFC1.FooClassS B1(mainY);
Console.WriteLi ne("mainY after SB1 is: {0}, int is: {1}",
mainY, myFC1.j);
myFC1.FooClassS B2(ref mainY);
Console.WriteLi ne("mainY after SB2 is: {0}, int is: {1}",
mainY, myFC1.j);
// myFC1.FooClassS B1(mainY2); //dangerous!: compiles but
gives runtime error

AClass1 A = new AClass1();
A.Aint = 111;
AClass1 B = new AClass1();
B.Aint = 222;
Console.WriteLi ne("main A,B before swap: {0}, {1}",
A.Aint, B.Aint);
myFC1.Swap(ref A, ref B);

Console.WriteLi ne("main A,B after swap: {0}, {1}", A.Aint,
B.Aint);
Console.WriteLi ne("........... .....");
Console.WriteLi ne("reset back to A=111, B=222");
A.Aint = 111; B.Aint = 222;
Console.WriteLi ne("main A,B before swap2: {0}, {1}",
A.Aint, B.Aint);
myFC1.Swap2(A, B);

Console.WriteLi ne("main A,B after swap2: {0}, {1}",
A.Aint, B.Aint);

}
}
}
//////////////////////
using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ParaPassingPass ByValByRef_01
{
class FooClass1
{
public FooClass1()
{
j = 101;
}
public int j;

public void FooClassSB1(Str ingBuilder x)
{
x.Append("SB1") ;
Console.WriteLi ne("inside FCSB1, part I: {0}", x);
j = j + 123;
StringBuilder temp = new
StringBuilder(" temp_string_loc al_only!");
x = temp;
Console.WriteLi ne("inside FCSB1, part II: {0}", x);
}
public void FooClassSB2(ref StringBuilder x)
{
x.Length = 0; //this truncates [sets] string to zero
length (nice feature)
x.Append("SB2!" );
Console.WriteLi ne("inside FCSB2, part I: {0}", x);
j = 22222;

StringBuilder temp = new
StringBuilder(" temp_string_NOT _just_local_onl y!");
x = temp;
Console.WriteLi ne("inside FCSB2, part II: {0}", x);

}

public void Swap(ref AClass1 a, ref AClass1 b)
{
AClass1 temp = a; //no need for ‘new’ for temp
instantiation
Console.WriteLi ne("inside Swap a: {0}", a.Aint);
a = b;
Console.WriteLi ne("inside Swap a, b, before b swap: {0},
{1}", a.Aint, b.Aint);
b = temp;
Console.WriteLi ne("inside Swap a,b, after swap: {0}, {1}",
a.Aint, b.Aint);

}

public void Swap2(AClass1 a, AClass1 b)
{
// this version, Swap2, does not get the job done since 'ref' not
being used
AClass1 temp = new AClass1();
temp = a;
Console.WriteLi ne("inside Swap2 a: {0}", a.Aint);
a = b;
b = temp;
Console.WriteLi ne("inside Swap2 a,b, after swap: {0},
{1}", a.Aint, b.Aint);

}
}
}

/*

*/
/////////////////////////////////
using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ParaPassingPass ByValByRef_01
{
class AClass1
{
public int Aint;

}
}

////////////////////////////////

// OUTPUT

Output of mainY is true or false?...True
value of string mainY is:Hellow
............... ...True
inside FCSB1, part I: Hellow SB1
inside FCSB1, part II: temp_string_loc al_only!
mainY after SB1 is: Hellow SB1, int is: 224
inside FCSB2, part I: SB2!
inside FCSB2, part II: temp_string_NOT _just_local_onl y!
mainY after SB2 is: temp_string_NOT _just_local_onl y!, int is: 22222
main A,B before swap: 111, 222
inside Swap a: 111
inside Swap a, b, before b swap: 222, 222
inside Swap a,b, after swap: 222, 111
main A,B after swap: 222, 111
............... .
reset back to A=111, B=222
main A,B before swap2: 111, 222
inside Swap2 a: 111
inside Swap2 a,b, after swap: 222, 111
main A,B after swap2: 111, 222
Press any key to continue . . .
Jul 3 '08 #17
On Jul 3, 2:28*pm, raylopez99 <raylope...@yah oo.comwrote:
If the parameter were passed by reference by default, that would work.
As it is, it does nothng.

This example is totally wrong Jon. *You probably meant to write
"b=tmp;"
Yes, thank you for the correction.
And you call yourself a "[C# MVP]"? *Well, I can call myself an "C#
N00b MVP" then.
Well, you'll still just look foolish - as well as potentially confuse
a few newcomers.
As you can see for the string (a reference type) passing by reference
is the same as passing by value when changing the string--and outside
the method the string *is* changed. *However, if 'new' were involved,
it would be different.
Where is your string test? Where do you supposedly "change" a string?
(Strings are immutable.)

You do admittedly have various log lines which *claim* to be about
strings ("value of string mainY") but as mainY is of type
StringBuilder rather than string, that doesn't really say much.

Jon
Jul 3 '08 #18
Please tell me you're kidding. You aren't passing a String; you are
passing a StringBuilder. This does not prove *anything* relating to a
string (which is what you claim). A StringBuilder is a mutable wrapper
(which as an implementation detail, tortures a string internally).
Again, what this comes down to is passing a reference to the object
(StringBuilder) . Try actually passing a *string* with/without "ref",
changing it inside the method, and see how far you get...

I'm not sure the swap example proves anything that we didn't already
know? The entire discussion of "new" is unrelated and purely
misleading/distracting to the actual discussion; the key thing is that
any *assignment* to the argument (be it to "new", to "null", or any
other value) - i.e. something that changes *the reference* is only
reflected to the caller with "ref". Which is the entire *point* of the
distinction of pass-by-reference vs reference-type.

Re MVP, the difference here is that it isn't the awardee who suddenly
decides that they are an MVP...

Marc
Jul 3 '08 #19
On Jul 3, 6:55*am, Marc Gravell <marc.grav...@g mail.comwrote:
Please tell me you're kidding. You aren't passing a String; you are
passing a StringBuilder. This does not prove *anything* relating to a
string (which is what you claim). A StringBuilder is a mutable wrapper
(which as an implementation detail, tortures a string internally).
OK, picky picky! StringBuilder is what I meant, which is similar in
some way to String (capital S), but not a 'string', which in C# is not
a recognized type outside of String (that is, 'string' is the same as
'String' in C#, apparently the first letter is not case sensitive,
i.e., the C# compiler developers overloaded this term).
Again, what this comes down to is passing a reference to the object
(StringBuilder) . Try actually passing a *string* with/without "ref",
changing it inside the method, and see how far you get...
I guess I would get the same as my demo program.
>
I'm not sure the swap example proves anything that we didn't already
know? The entire discussion of "new" is unrelated and purely
misleading/distracting to the actual discussion; the key thing is that
any *assignment* to the argument (be it to "new", to "null", or any
other value) - i.e. something that changes *the reference* is only
reflected to the caller with "ref". Which is the entire *point* of the
distinction of pass-by-reference vs reference-type.
Yes, I'm sure your correct *in your mind's eye*. But from the prose
you type, it's confusing. The point I made with the demo program is
that 'changes' the reference implies something happens to the
reference--and that something is permanent outside the method/function
calling it and the object it points to. 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. This was
demonstrated in my program--which is copyright by the way-- in the
line: x.Append("SB1") ; The StringBuilder variable mainY is assigned,
permanently, the value "SB1" even though mainY was passed (or a
reference to the same, if you're pedantic) to the function/method 'by
value' rather than 'by reference'.
>
Re MVP, the difference here is that it isn't the awardee who suddenly
decides that they are an MVP...

Marc
OK, OK, I'll stop using "N00b" then, if that's the problem. I'll put
a different disclaimer in.

Ray Lopez
[C# MVP] <--kinda, sorta, "in the mind's eye"
Jul 3 '08 #20

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

Similar topics

5
5715
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
2163
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
2441
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
5800
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
2414
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
2207
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
1965
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
2571
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
8295
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
5165
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
8761
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
9281
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...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
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
4525
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...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.