Left and right value of assignment for functions returning reference types

M

muler

Hi all,

This is an excerpt from the Book "Programming Microsoft Visual C#: The
Language":

A function evaluates to the return value. When a reference type is
returned, a function is available as a left- or right-value of an
assignment. Functions that return a value type are restricted to
right-values of an assignment.

To test that statment I did sth like this:

class Program
{

static int Add(int x, int y)
{
return x + y;
}

static string AddS(string i, string j)
{
return i + j;
}

static void Main(string[] args)
{
//Add(2, 3) = 5; // this is an error - good
AddS("mulu", "geta") = "thomas"; // why is this an
error?
}
}

Can you help?

Thanks in advance
 
B

Bill Butler

muler said:
Hi all,

This is an excerpt from the Book "Programming Microsoft Visual C#: The
Language":

A function evaluates to the return value. When a reference type is
returned, a function is available as a left- or right-value of an
assignment. Functions that return a value type are restricted to
right-values of an assignment.

I think the author is wrong.
I tried to hunt around in the C# spec to see where this comment
originated, but I couldn't find anything even close.
The only mention of lvalues (variable-reference in the spec), in
relation to methods, has to do with ref/out parameters.

If I may speculate a bit, take the following:

Foo() = "Bar"; // sample of what the Author is saying is OK

this is (sort of ) equivalent to

string temp = Foo();
temp = "Bar";

In other words, you are assigning the string to a temporary variable.
Even if it was allowed, I am sure it would be doing what you expected.

Bill
 

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