if we have a 1D array, we could use the following to see if all elements are equal to 3:
int[] t = Enumerable.Repeat(3, 10).ToArray();
if (t.All(item => item.Equals(3))) MessageBox.Show("all elements equals to 3");
but if I have a 2D-array, how could I check if all elements are equal to 3 ( without any for-Loops ):
int[,] t2D= new int[,] { { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 } };
if( CHECK IF ALL ELEMENTS IN **t2D** are equal to 3)
{
MessageBox.Show("all elements equals to 3");
}
What should I put in If-statement?
2D-array is an enumerable type (but it implements non-generic IEnumerable
). And it's enumerator enumerates over all items in 2D-array. So, only thing you need to do - cast its items to int
(thus retrieving IEnumerable<int>
) and apply All
t2D.Cast<int>().All(x => x == 3)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.