2012-11-09 54 views
-3

二維數組分配我想知道如何檢查二維字符串數組用於分配在C#檢查在C#

string[][] mString; bool empty = string.IsNullOrEmpty(mString);

不會做的伎倆。一些幫助?

+0

你要確認任何字符串爲空/空或所有的人都? – lstern

+0

不,我想知道這個數組是否被賦值。任何想法,如何繼續? –

+0

mString!= null && mString.Length> 0 – lstern

回答

3

你想要檢查什麼?

不必2- dimensioanl陣列(這將是[,]),但交錯數組陣列陣列的-。

所以,你可以寫:

bool empty = mString == null;    // the whole (outer) array 

// 1+ sub-arrays is null? 
bool empty = (mString == null) || mString.Any(a => a == null)) ; 

// any string is null or empty 
bool empty = (mString == null) 
     || mString.Any(a => a == null)) 
     || mString.Any(a => a.Any (s => string.IsNullOrEmpty(s)); 
+0

ups,我不知道它是一個鋸齒狀的陣列。所以實際上我喜歡序列化一個鋸齒狀的數組,因此我必須提前檢查它是否已初始化。 –

+0

再一次,「初始化」是什麼意思? –

+0

嗯,另一個例子可能解釋我試圖實現,考慮: 'int mInt; bool empty =!mInt.Equals(0); Console.WriteLine(empty); ' 在這裏我可以檢查整數值是否被賦值。現在爲鋸齒陣列) –