3

I have 2 array of object. 1st array of object have property which I want to copy to other array.

1st array of object

HotelRoomResponse[] hr=new HotelRoomResponse[100];

2nd array of object

RateInfos[] rt = new RateInfos[100];

now what i want to do is copy a property of 1st array like

rt=hr[].RateInfo;

but it give error. What is correct way to do this????

2
  • How does RateInfos relate to HotelRoomResponse Commented Nov 7, 2013 at 6:59
  • want moar equal answerz! Commented Nov 7, 2013 at 7:02

3 Answers 3

6

You can't just project an array like that. You effectively have to loop - although you don't need to do that manually in your own code. LINQ makes it very easy, for example:

RateInfos[] rt = hr.Select(x => x.RateInfo).ToArray();

Or you could use Array.ConvertAll:

RateInfos[] rt = Array.ConvertAll(hr, x => x.RateInfo);

In both of these cases there's still a loop somewhere - it's just not in your code.

If you're quite new to C# and don't understand LINQ, lambda expressions, delegates etc yet, then you could just write the code yourself:

RateInfos[] rt = new RateInfos[hr.Length];
for (int i = 0; i < rt.Length; i++)
{
    rt[i] = hr[i].RateInfo;
}

All of these three will achieve the same result.

  • The first approach is probably the most idiomatic in modern C#. It will work with any input type, and you can change from ToArray() to ToList() to get a List<RateInfos> instead of an array, etc.
  • The second approach is slightly more efficient than the first and will work with .NET 2.0 (whereas LINQ was introduced in .NET 3.5) - you'll still need a C# 3 compiler or higher though. It will only work as written with arrays, but there's a similar ConvertAll method for List<T>.
  • The third approach is the most efficient, but obviously more code as well. It's simpler for a newcomer to understand, but doesn't express what you're trying to achieve as clearly when you know how all the language features work for the first two solutions.
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for Array.ConvertAll. See your you'll still need a C# 3 compiler note
@IlyaIvanov: It's .NET 2.0 compatible. It was introduced in C# 3 (there's no such thing as C# 3.5), but you can still use it when targeting .NET 2.0. I chose the wording in my answer carefully :)
there's no such thing as C# 3.5 sure, I've meant .net 3.5. Anyway, marvelous answer, as usual.
@IlyaIvanov: Right - but the key is that lambda expressions are a C# feature, not a .NET library or CLR feature. That's why it's crucial to separate language versions from CLR or library versions :)
5
RateInfos[] rt = hr.Select(item => item.RateInfo).ToArray();

2 Comments

@DeepakRaghav: Do you understand how it's working though? Have you used LINQ? It's worth doing some reading to make sure you know what's going on here.
yeah i used LINQ.Not much but enough to understand the answer.
2

Use LINQ:

RateInfos[] rt = hr.Select(x => x.RateInfo).ToArray();

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.