tags:

views:

422

answers:

4

In Ruby you can use the map/collect method on an array to modify it:

a = [ "a", "b", "c", "d" ]
a.collect! {|x| x + "!" }
a                            #=>  [ "a!", "b!", "c!", "d!" ]

Is there a simple way to do this in C#?

+4  A: 
a = a.Select( s => s + "!" ).ToArray();
Tanzelax
Perfect! Thanks for the quick response!
c00lryguy
be aware that this creates a new array and does not modify original array, as asked in OP's question.
spender
@spender: Very true. 'a =' in the beginning only takes care of making this particular reference point to the new array, if there's other references to the original array, they won't be updated.
Tanzelax
c00lryguy
A: 

you may try this

var a = new[] { "a", "b", "c", "d" };

a = a.Select(p => p + "!").ToArray();

ldsenow
A: 

Yup, using Linq (but this won't modify the original collection)

var a=new[]{"a","b","c","d"};
a.Select(x=>x+"!");
spender
+1  A: 

I prefer using ConvertAll since it's quicker and I believe more intuitive.

var a = a.ConvertAll(x => x + "!").ToArray();
mythz