3

lets consider this snippet of C# code:

     List<string> a = new List<string>();
     a.Add("word1");
     a.Add("word2");

now lets say I want to have "word3" instead of word2. I can say

     a[1]="word2";

but if I try

     a.ElementAt(2) ="word2"; 

then I get an error saying the left hand side must be a variable.

this syntax behaves in the same way for reference types and value types. can someone explain why the second syntax is flawed?

10
  • 2
    First thing is indexer with set accessor. Second thing is a method, which return some value. Commented Oct 16, 2015 at 19:56
  • 2
    The syntax is not flawed, you need to see the documentation for ElementAt, it will return you the element at the specified position. Commented Oct 16, 2015 at 19:57
  • 1
    @IIIIIIIIIIIIIIIIIIIIIIII Clearly, regardless of whether it should or not, it doesn't, as the error message is telling you. Commented Oct 16, 2015 at 20:05
  • 2
    Think of it this way, ElementAt(2) returns a string value, word2, and you are trying to assign to it, so you are doing the equivalent of "word2" = "word3", which isn't allowed. Assignments can only target variables, not values themselves. Commented Oct 16, 2015 at 20:05
  • 1
    @M.kazemAkhgary But that's not what you said what true. The OP was stating that he assumed that the method returned a variable, rather than a value, and that is only possible through the use of a pointer. Even if the type is a reference type, you're returning a value of a reference, rather than a reference to a reference. This is made clear by the fact that the method in the question is returning a reference type, but it's not a reference to that value (which is a reference) it's just the value of that reference, hence the error. Commented Oct 16, 2015 at 20:13

6 Answers 6

9

This is a method call which returns a value:

a.ElementAt(2)

You can store a value in a variable and assign other values to that variable. But the method call itself can't be assigned to. A method call isn't a container to which a value can be assigned.

The array indexer directly references the element in the array (collection) variable, to which you can assign something. The method call doesn't reference anything, it returns something.

So you can assign to a variable:

x = "some value";

Even an index of a collection variable:

x[0] = "some value";

But not to a method call:

SomeMethod() = "some value";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you David, my understanding was flawed, I thought .ElementAt(2) will return me a reference to the object so I can assign new object.
2

ElementAt is a method and you can not assign value to it.

You can use return value of it, it returns the element at a specified index in a sequence:

var x= a.ElementAt(0);

Comments

0
 a.ElementAt(2) ="word2"; 

Here you are calling a method and getting a result, then trying to assign a result. Not allowed. If you had this:

var foo = a.ElementAt(2);
foo = "word2";

then that's fine.

When you use a[1]="word2"; then some syntactic sugar is happening. You are calling a method on the list, not simple assignment. Be aware that depending how the list is implemented, accessing an element by index might be an expensive operation.

In short use arrays for fixed lengths lists, list for a collection that can change size.

1 Comment

Should make clear that in my first code example, the value in the list doesn't change, foo is assigned to a new string.
0

The = is an assignment operator. It assigns the value of what is on the right side to what on the left side.

There is a restriction on what can be on the left side.

Referencing from this MSDN reference:

The left operand of an assignment must be an expression classified as a variable, a property access, an indexer access, or an event access.

ElementAt() is a method call so it cannot be on the left side of an assignment operator.

Comments

0

Because, ElementAt is only to get the element of specified index, you cannot assign it (https://msdn.microsoft.com/en-us/library/bb299233(v=vs.110).aspx)

You could try remove item at index and insert new item like this:

a.RemoveAt(2);
a.Insert(2, "word3");

Comments

0

In addition to the other answers, apart from it being a method and not a variable, note that the method operates on an IEnumerable<T>. In this particular case you're trying to assign a value to a list, but ElementAt is not restricted to list, but can operate on any sequence really.

List<T> has an indexer and it has a setter, so it can be assigned. But other classes that also implement IEnumerable<T> may not, even though they can be used with ElementAt, so in these cases it may not even make sense to try an assignment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.