ref assignment

M

MRe

Hi,

Why doesn't this work (it gives the error 'A ref or out argument must be
an assignable variable').

public void Caller()
{
int Value;
Callee(ref Value = 5); // <-- Error here under Value = 5; it's
assignable, look at me assign, woo!
}
public void Callee(ref int Value)
{
MessageBox.Show(Value.ToString());
}

..is ref evaluated before any operators (It also doesn't work with
Callee(ref (Value = 5));)?

I'm using VS2005; .NET2.0.

Thank you,
Eliott
 
N

Nicholas Paldino [.NET/C# MVP]

Elliot,

I would think that 5 is not assignable.

If anything, even if this syntax is legal (which I don't think it is),
it isn't very clean. I would avoid it.
 
N

Noah Sham

The result of Value = 5 is not a reference to 'Value' it is a copy of
'Value' and that copy cannot be passed by reference.

Contrast this with this method
public void Callee(int value)
{
}
with this signature 'Callee(Value=5)' will succeed because a copy of an int
is expected.
 
M

MRe

Thank you for the reply Noah,

| The result of Value = 5 is not a reference to 'Value' it is a copy of
| 'Value' and that copy cannot be passed by reference.

Does that mean that every time an assignment is made to a simple type, it
does the assignment to the variable on the stack (does C# use a stack?), and
then, also creates a new copy to return as a result of the operator?

Thanks again,
Eliott


| The result of Value = 5 is not a reference to 'Value' it is a copy of
| 'Value' and that copy cannot be passed by reference.
|
| Contrast this with this method
| public void Callee(int value)
| {
| }
| with this signature 'Callee(Value=5)' will succeed because a copy of an
int
| is expected.
|
|
| "MRe" <mreatdublindotie> wrote in message
| | > Hi,
| >
| > Why doesn't this work (it gives the error 'A ref or out argument must
be
| > an assignable variable').
| >
| > public void Caller()
| > {
| > int Value;
| > Callee(ref Value = 5); // <-- Error here under Value = 5; it's
| > assignable, look at me assign, woo!
| > }
| > public void Callee(ref int Value)
| > {
| > MessageBox.Show(Value.ToString());
| > }
| >
| > ..is ref evaluated before any operators (It also doesn't work with
| > Callee(ref (Value = 5));)?
| >
| > I'm using VS2005; .NET2.0.
| >
| > Thank you,
| > Eliott
| >
| >
|
|
 
N

Noah Sham

C# has value types and reference types. The value types are stack based and
the reference types are heap based with the reference pointer on the stack.
double, byte, char, int32, int64 are value types as are structs constants
and enumerations.

In Callee(Value = 5) 'Value=5' is an expression that assigns 5 to Value and
returns a copy of Value.
int xyz = (Value = 5)
The compiler realizes that it is an expression and not a variable reference.
 
M

MRe

Ah, okay - I was thinking I was doing like c++..

void callee(int* Value);
callee(&(Value = 5));

...buts no, I wasn't.. Hey! Did you say constants go on the stack - that's
crazy! Well, I guess they got to go somewhere. Anyway..

It looked like an annoying peculiarity, but it makes sense now, so I can
live with it.
Thank you very much for you help Noah :)
Eliott
 
J

Jon Skeet [C# MVP]

Why doesn't this work (it gives the error 'A ref or out argument must be
an assignable variable').

The expression Value=5 is classified as a value (the result of the
assignment) not a variable. Without getting "ref" involved, it's the
same reason you can't do:

int value;

(value = 5) = 10;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top