Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# ASP.NET MVC Basics Views Displaying a List with Razor

Joel Brennan
Joel Brennan
18,300 Points

Is there any difference between testing if(artists.Length > 0) and if(artists.Any()) ? If so what is the difference?

Would there be any situations where one would be preferred over the other method of testing for an empty array?

1 Answer

Steven Parker
Steven Parker
231,198 Points

:point_right: These produce the same result, but using different methods.

In the case of "artists.Length > 0", Length is a built-in property of the array. But in the case of "artists.Any()", you're using a LINQ extension method on the array.

Since an array always has Length, but you might not always be using LINQ, I would consider the former preferable for simply testing if the array is empty.