2010-06-08 51 views
0

有什麼辦法來初始化動態字符串數組。我們可以初始化動態字符串數組


public class CXmlFileHook 
    { 
     string vAppname; 
     string vClassname; 
     string vIdmark; 
     string vExecname; 
     string [] vApiName; 
     string vCtor;

public CXmlFileHook() 
    { 
     this.vAppname = "Not Set"; 
     this.vIdmark = "Not Set"; 
     this.vClassname = "Not Set"; 
     this.vExecname = "Not Set"; 
     this.vApiName = new string[9] { "Not Set", "Not Set", "Not Set", "Not Set", "Not Set", "Not Set", "Not Set", "Not Set" ,"Not Set"}; 

     this.vCtor = "CXmlFileHook()"; 

    } 

現在我想字符串的大小應增加動態,並根據它的大小初始化它的自我是有可能????

+0

您的問題尚不清楚 - 請提供更多詳情。另外,你爲什麼讓它成爲社區維基? – 2010-06-08 06:45:24

+0

什麼是動態字符串數組? '列表'? – 2010-06-08 06:55:42

回答

2

仍然不知道你真正需要知道的,所以這裏有幾個答案從:-)選擇在C#

數組不能改變其長度。如果您需要動態集合,請使用集合類,例如。 List<T>

一個List<T>可以使用相同的語法進行初始化:

this.vApiName = new List<string> 
    { 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set" 
    }; 

如果數組的長度在運行時不改變,你可以使用它。在聲明和初始化數組時,不需要指定數組的長度。長度是由編譯器決定(它仍然在運行時間常量):

this.vApiName = new string[] // <= no array-length, set to 9 by compiler 
    { 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set", 
    "Not Set" 
    }; 

你並不需要初始化默認值,如果你只是想用null(見this link)來初始化。

this.vApiName = new string[9]; // array containing 9 x null