In Ruby you can use the map/collect method on an array to modify it:
a = [ "a开发者_StackOverflow中文版", "b", "c", "d" ]
a.collect! {|x| x + "!" }
a #=> [ "a!", "b!", "c!", "d!" ]
Is there a simple way to do this in C#?
a = a.Select( s => s + "!" ).ToArray();
I prefer using ConvertAll since it's quicker and I believe more intuitive.
var a = a.ConvertAll(x => x + "!").ToArray();
Since I prefer the naming, I personally use my own Enumerable Map Extension methods which is available to all IEnumerable<T>
Types, guards against null
and follows the standing naming in other languages for functinoal projection.
var to = a.Map(x => x + "!");
you may try this
var a = new[] { "a", "b", "c", "d" };
a = a.Select(p => p + "!").ToArray();
Yup, using Linq (but this won't modify the original collection)
var a=new[]{"a","b","c","d"};
a.Select(x=>x+"!");
精彩评论