How to use Array.ConvertAll to convert an array of one type to an array of another type

Priya
Priya
Member
62 Points
26 Posts

Hi,

I am trying to convert an array of one object type to array of another object type. I got success by using for loop. But now I have Array.ConvertAll and want to use it. How can I do it usning Array.ConvertAll.

Thanks in advance.

Views: 8165
Total Answered: 1
Total Marked As Answer: 1
Posted On: 12-Apr-2016 19:38

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Moderator
1172 Points
211 Posts
         

Syntax:

Array.ConvertAll<TInput, TOutput> Method (TInput[], Converter<TInput, TOutput>)

It converts an array of one type to an array of another type.

Example:

In the following example we have two class InputClass and OutputClass , one method ConvertArrayObject that will convert array of InputClass to array of OutputClass. 

public class InputClass {
public int id { get; set; }
public string name { get; set; }
}
public class OutputClass {
public string myId { get; set; }
public string myName { get; set; }
}
public OutputClass[] ConvertArrayObject(InputClass[] inputArray) {
return
System.Array.ConvertAll<InputClass, OutputClass>(inputArray, (elem) => {
return new OutputClass {
myId = elem.id.ToString(),
myName = elem.name
};
});
}

 

Posted On: 14-Apr-2016 00:38
 Log In to Chat