Share via

what is the best practice to change the element of IEnuerable?

mc 6,761 Reputation points
2026-02-08T09:34:15.7633333+00:00

I have IEnumerable<XY> and how to change all the elements in it?

class XY
{
   public int X{get;set;}
public int Y{get;set;}
}

what I want is to adding 1 of the X

I have to change it to array ?

var arr=xys.ToArray();
for(int i=0;i<arr.Length;i++)
{
      arr[i].X+=1;
}


Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Jack Dang (WICLOUD CORPORATION) 14,020 Reputation points Microsoft External Staff Moderator
    2026-02-09T04:02:32.35+00:00

    Hi @mc ,

    Thanks for reaching out.

    IEnumerable<T> only describes how you read items, not how they are stored. It means you can iterate through the items, but you can’t add, remove, or replace elements in the collection itself.

    However, in your case, XY is a class (a reference type). That means the objects inside the IEnumerable<XY> are real instances, not copies. So while you can’t change the collection, you can safely modify the properties of each XY object.

    You don’t need to convert it to an array or list just to update X. A simple iteration is enough:

    foreach (var xy in xys)
    {
        xy.X += 1;
    }
    

    Converting to an array with ToArray() only makes sense if you need indexed access or want to change which elements are in the collection. For just updating properties, it adds unnecessary overhead.

    The only time this would not work is if XY were a struct. In that case, each iteration would give you a copy, and changes wouldn’t persist. Since XY is a class here, you’re good.

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 83,421 Reputation points Volunteer Moderator
    2026-02-08T16:32:03.3+00:00

    Your enumerable is a collection of objects, and you are updating a property value of the objects, not replacing the object in the collection, so as suggested the most efficient approach is a foreach().

    0 comments No comments

  2. Viorel 126.7K Reputation points
    2026-02-08T12:23:15.9833333+00:00

    IEnumerable means that the elements can be enumerated:

    IEnumerable<XY> elements = . . .
    
    foreach( XY element in elements ) ++element.X;
    

    Using LINQ extensions:

    elements.All( e => { ++e.X; return true; } );
    
    0 comments No comments

  3. Q&A Assist
    2026-02-08T09:34:26.99+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To modify elements in an IEnumerable<T>, you typically need to convert it to a mutable collection, such as an array or a list, because IEnumerable<T> itself does not support direct modification of its elements. Your approach of converting the IEnumerable<XY> to an array using ToArray() is a valid method to achieve this. Once you have the array, you can iterate through it and modify the properties of the objects as needed.

    Here’s a concise way to do it:

    var arr = xys.ToArray();  // Convert IEnumerable<XY> to an array
    for (int i = 0; i < arr.Length; i++)
    {
        arr[i].X += 1;  // Increment the X property of each XY object
    }
    

    Alternatively, if you want to keep the original IEnumerable<XY> intact and create a new collection with the modified values, you could use LINQ to project the changes into a new collection:

    var modifiedXys = xys.Select(xy => new XY { X = xy.X + 1, Y = xy.Y });
    

    This way, you maintain immutability of the original collection while creating a new one with the desired modifications.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.