![](/img/trans.png)
[英]C#: How to check if all the elements in an array of boolean variables are of the same value (All true or All false)?
[英]Check if ALL elements in 2D-Array have same value
如果我們有一維數組,我們可以使用以下內容來查看所有元素是否等於3:
int[] t = Enumerable.Repeat(3, 10).ToArray();
if (t.All(item => item.Equals(3))) MessageBox.Show("all elements equals to 3");
但如果我有一個2D數組,我怎么能檢查所有元素是否等於3(沒有任何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");
}
我應該在If語句中添加什么?
2D數組是一種可枚舉的類型 (但它實現了非通用的IEnumerable
)。 它的枚舉器枚舉了2D數組中的所有項目。 所以,只需要做的事情 - 將其項目轉換為int
(從而檢索IEnumerable<int>
)並應用All
t2D.Cast<int>().All(x => x == 3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.