2013-11-20 36 views
-4
class abc 
{ 
    public object test(params object[] par) 
    { 
    //I need Count of the parameter here which means to check par contains 1 or 1,2 
    } 

} 

I access the class like, 

abc obj =new abc(); 
obj.test(1); 
    (or) 
obj.test(1,2); 

我的問題是,有可能發送1或1,2。我需要計數在測試類中的對象有多少個參數?如何執行此操作?在C中獲取參數計數#

+3

我不禁疑惑,爲什麼你不嘗試訪問數組的長度? –

回答

8

使用Array.Length屬性。

public object test(params object[] par) 
    { 
     var count = par == null ? 0 : par.Length; 
    } 
1

您可以使用對象數組的Length屬性。

public object test(params object[] par) 
{   
    int length = par == null ? 0 : par.Length; 
} 
0

可以使用Length財產
試試這個:

int len=par.Length;