Adam Lee
Adam Lee

Reputation: 25768

Is there anything similar in C#/Java to do the following?

In Javascript(JQuery), we can use map method to convert an array to another using a callback method. This is a great advantage of FP.

I am wondering if we can do the same thing in C# or Java?

Upvotes: 3

Views: 93

Answers (2)

Paul-Jan
Paul-Jan

Reputation: 17278

In C#, Array has the ConvertAll method that literally does what you are asking for.

However, it takes a delegate as the argument, so for a concise way of writing it you'll want to use a lambda expression, making it quite similar to the projection as suggested by devdigital.

Upvotes: 0

devdigital
devdigital

Reputation: 34359

One option is to use a projection with LINQ:

myCollection.Select(element => new MyOtherType { ... });

Upvotes: 5

Related Questions