2011-07-13 73 views

回答

13
var allAreSame = list.All(x => x == list.First()); 
3
var allAreSame = list.Distinct().Count() == 1; 

或更多一點的最佳

var allAreSame = list.Count == 0 || list.All(x => x == list[0]); 
+1

這是矯枉過正! –

0
var hasIdenticalItems = list.Count() <= 1 || list.Distinct().Count() == 1; 
1

如何:

string[] s = { "same", "same", "same" }; 
    if (s.Where(x => x == s[0]).Count() == s.Length) 
    { 
     return true; 
    } 
+0

這是lambda表達式嗎? –

相關問題