0

Suppose I have a class like the following:

class Book
{
    public int id;
    public string title;
}

And somewhere later I have an array Book[] books and now want an array of the titles string[] titles = {books[0].title, books[1].title, ..., books[n].title}. Is there an easier way than looping over the books array? Something like

string[] titles = books.getProperty(title)

Thanks in advance

1 Answer 1

5

Generally, you would use Linq methods to manipulate collections, for example (Select & ToArray):

var titles = books.Select(x => x.title).ToArray();

However, since this is an array and you an array as a result, you can also use some static methods on the Array type (ConvertAll):

var titles = Array.ConvertAll(books, x => x.title);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.