Extension method question. Why do I need to use someObj = someObj.somemethod();

Posted by Kettenbach on Stack Overflow See other posts from Stack Overflow or by Kettenbach
Published on 2010-05-25T19:06:54Z Indexed on 2010/05/25 19:11 UTC
Read the original article Hit count: 216

Filed under:
|
|
|

Hi All, I have a simple extension method that I would like to use to add an item to an array of items.

    public static T[] addElement<T>(this T[] array, T elementToAdd)
    {

        var list = new List<T>(array) {elementToAdd};
        return list.ToArray();
    }

this works ok, but when I use it, I am having to set the array equal to the return value. I see that I am returning an Array. I likely want this method to be void, but I would like the item added. Does anyone have any ideas on what I need to do , to make this work the way I am wanting?

Instead of someArray = someArray.addElement(item), I just want to do someArray.addElement(item) and then someArray be ready to go. What am I missing here?

Thanks, ~ck in San Diego

© Stack Overflow or respective owner

Related posts about c#

Related posts about arrays