2010-01-04 73 views
5

因爲我知道如果我宣佈字典,我可以調用myDict.Clear()重用。如何重用StringBuilder obj?

現在,如果我宣佈某人作爲StingBuilder obj。

StringBuilder sb = new StringBuilder(); 

如何重用sb?謝謝。

實際上我需要爲mainDict打印所有可能的條件。

某人表達一個這樣的(在下面的代碼inclued)

sb.AppendFormat("{0}/{1}/{2}/{3}, {4}", pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair4.Value); 
Console.WriteLine(sb.ToString()); 

如果我宣佈了大量的StringBuilder OBJ文件的,我仍然無法檢測是多少OBJ文件對我來說足夠。實際上mainDict非常複雜。上面的代碼只是一個練習。謝謝。


代碼在一月04

更新
using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Text; 


class test 
{ 
    private static Dictionary<string, object> mainDict = new Dictionary<string, object>(); 

    public static void Main() 
    { 
     Dictionary<string, object> aSubDict = new Dictionary<string,object>(); 
     Dictionary<string, object> aSub1Dict = new Dictionary<string, object>(); 
     Dictionary<string, object> aSub2Dict = new Dictionary<string, object>(); 
     Dictionary<string, object> aSub3Dict = new Dictionary<string, object>(); 
     Dictionary<string, object> aSub4Dict = new Dictionary<string, object>(); 

     mainDict.Add("ADKey", aSubDict); 
     mainDict.Add("ASKey", "AValue"); 
     aSubDict.Add("BDKey", aSub1Dict); 
     aSubDict.Add("BSKey", "BValue"); 
     aSub1Dict.Add("CDKey", aSub2Dict); 
     aSub1Dict.Add("CSKey", "CValue"); 
     aSub2Dict.Add("DDKey",aSub3Dict); 
     aSub2Dict.Add("DSKey", "DValue"); 
     aSub3Dict.Add("EDKey", aSub4Dict); 
     aSub3Dict.Add("ESKey", "EValue"); 
     aSub4Dict.Add("FKey", "FValue"); 


     StringBuilder sb; 

     foreach (KeyValuePair<string, object> pair1 in mainDict) 
      // watch out for NullReferenceException 
      if (!ReferenceEquals(null, mainDict[pair1.Key]) && (mainDict[pair1.Key] is string)) 
      { 
       Console.WriteLine("Key = {0}, Value = {1}", pair1.Key, pair1.Value); 
       sb = new StringBuilder(); 
       sb.AppendFormat("{0}, {1}", pair1.Key, pair1.Value); 
       Console.WriteLine(sb.ToString()); 
      } 
      // IDictionary is not the one from the Generics namespace, it is the one from the System.Collections namespace 
      else if (!ReferenceEquals(null, mainDict[pair1.Key]) && (mainDict[pair1.Key] is Dictionary<string, object>)) 
      { 
       foreach (KeyValuePair<string, object> pair2 in (Dictionary<string, object>)pair1.Value) 
        if (!ReferenceEquals(null, ((Dictionary<string, object>)pair1.Value)[pair2.Key]) && (((Dictionary<string, object>)pair1.Value)[pair2.Key] is string)) 
        { 
         Console.WriteLine("SubKey = {0}, Value = {1}", pair2.Key, pair2.Value); 
         sb = new StringBuilder(); 
         sb.AppendFormat("{0}/{1}, {2}", pair1.Key, pair2.Key, pair2.Value); 
         Console.WriteLine(sb.ToString()); 
        } 
        else if (!ReferenceEquals(null, ((Dictionary<string, object>)pair1.Value)[pair2.Key]) && (((Dictionary<string, object>)pair1.Value)[pair2.Key] is Dictionary<string, object>)) 
        { 
         foreach (KeyValuePair<string, object> pair3 in (Dictionary<string, object>)pair2.Value) 
          if (!ReferenceEquals(null, ((Dictionary<string, object>)pair2.Value)[pair3.Key]) && (((Dictionary<string, object>)pair2.Value)[pair3.Key] is string)) 
          { 
           Console.WriteLine("SubKey = {0}, Value = {1}", pair3.Key, pair3.Value); 
           sb = new StringBuilder(); 
           sb.AppendFormat("{0}/{1}/{2}, {3}", pair1.Key, pair2.Key, pair3.Key, pair3.Value); 
           Console.WriteLine(sb.ToString()); 
          } 
          else if (!ReferenceEquals(null, ((Dictionary<string, object>)pair2.Value)[pair3.Key]) && (((Dictionary<string, object>)pair2.Value)[pair3.Key] is Dictionary<string, object>)) 
          { 
           foreach (KeyValuePair<string, object> pair4 in (Dictionary<string, object>)pair3.Value) 
            if (!ReferenceEquals(null, ((Dictionary<string, object>)pair3.Value)[pair4.Key]) && (((Dictionary<string, object>)pair3.Value)[pair4.Key] is string)) 
            { 
             Console.WriteLine("SubKey = {0}, Value = {1}", pair4.Key, pair4.Value); 
             sb = new StringBuilder(); 
             sb.AppendFormat("{0}/{1}/{2}/{3}, {4}", pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair4.Value); 
             Console.WriteLine(sb.ToString()); 
            } 
            else if (!ReferenceEquals(null, ((Dictionary<string, object>)pair3.Value)[pair4.Key]) && (((Dictionary<string, object>)pair3.Value)[pair4.Key] is Dictionary<string, object>)) 
            { 
             foreach (KeyValuePair<string, object> pair5 in (Dictionary<string, object>)pair4.Value) 
              if (!ReferenceEquals(null, ((Dictionary<string, object>)pair4.Value)[pair5.Key]) && (((Dictionary<string, object>)pair4.Value)[pair5.Key] is string)) 
              { 
               Console.WriteLine("SubKey = {0}, Value = {1}", pair5.Key, pair5.Value); 
               sb = new StringBuilder(); 
               sb.AppendFormat("{0}/{1}/{2}/{3}/{4}, {5}", pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair5.Key, pair5.Value); 
               Console.WriteLine(sb.ToString()); 
              } 
              else if (!ReferenceEquals(null, ((Dictionary<string, object>)pair4.Value)[pair5.Key]) && (((Dictionary<string, object>)pair4.Value)[pair5.Key] is Dictionary<string, object>)) 
              { 
               foreach (KeyValuePair<string, object> pair6 in (Dictionary<string, object>)pair5.Value) 
                if (!ReferenceEquals(null, ((Dictionary<string, object>)pair5.Value)[pair6.Key]) && (((Dictionary<string, object>)pair5.Value)[pair6.Key] is string)) 
                { 
                 Console.WriteLine("SubKey = {0}, Value = {1}", pair6.Key, pair6.Value); 
                 sb = new StringBuilder(); 
                 sb.AppendFormat("{0}/{1}/{2}/{3}/{4}/{5}, {6}", pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair5.Key, pair6.Key, pair6.Value); 
                 Console.WriteLine(sb.ToString()); 
                } 
                else if (!ReferenceEquals(null, ((Dictionary<string, object>)pair5.Value)[pair6.Key]) && (((Dictionary<string, object>)pair5.Value)[pair6.Key] is Dictionary<string, object>)) 
                { 
                 Console.WriteLine("sub Dict Found"); 
                } 
              } 
            } 
          } 
        } 
      }   
    } 

} 

輸出這樣

SubKey = FKey, Value = FValue 
ADKey/BDKey/CDKey/DDKey/EDKey/FKey, FValue 
SubKey = ESKey, Value = EValue 
ADKey/BDKey/CDKey/DDKey/ESKey, EValue 
SubKey = DSKey, Value = DValue 
ADKey/BDKey/CDKey/DSKey, DValue 
SubKey = CSKey, Value = CValue 
ADKey/BDKey/CSKey, CValue 
SubKey = BSKey, Value = BValue 
ADKey/BSKey, BValue 
Key = ASKey, Value = AValue 
ASKey, AValue 

回答

17

您可以將Length設置爲0在.NET 4.0中有一個Clear()方法了。作爲文檔爲Clear狀態:

清除是一個方便的方法是 相當於當前實例的長度 屬性設置爲0 (零)。

因此,這不是什麼大不了的事:)

我會親自避免這樣做,除非你真的需要,但 - 我通常只需要創建一個新的StringBuilder。在我看來,這更容易理解 - 它更清楚地表明,您不再需要上一個對象的任何。

你有什麼特別的理由想重用這個對象嗎?如果是出於性能原因,您是否衡量了性能,並發現這是一個瓶頸?我想如果你有一個容量非常大的實例,並且你想避免再次分配它,這可能會很重要......但是這對我來說就像是一個邊緣案例。

(所有這一切都適用於字典以及,順便說一句,我不記得我最後一次清除一本字典。)

+0

嗨,喬恩,我附上我的示例代碼。我想我不能創建很多新的StringBuilder objs ....如果mainDict改變了,我不知道確切的數字,我的mainDict會動態地改變... – 2010-01-04 08:03:47

+0

爲什麼你認爲你不能創建很多新的StringBuilder對象?我並不是建議聲明更多的StringBuilder *變量* - 但是當你想要一個新的StringBuilder時,只需使用'sb = new StringBuilder()'。很難理解你的代碼試圖做什麼,恐怕... – 2010-01-04 08:19:31

+0

mainDict從XSD文件的解析結果創建,由其他團隊維護的XSD文件。我需要在編譯時創建另一個xsd2cs.cs文件(string [] map並鍵入[] map)。這就是爲什麼我需要打印出所有由'/'分隔的相關元素作爲連接字符串。那麼另一個運行時程序可以基於.cs文件驗證相關的XML。 – 2010-01-04 08:25:40

2

我想而不是重用exting StringBuilder對象,你應該創建一個新的StringBuilder目的。

+2

任何原因爲什麼?我沒有看到創建新實例來重新使用現有實例的優勢。這取決於上下文。 – 2010-01-04 08:03:21

+0

@Upul,對於我的情況,我想我寧願重新使用現有的實例。更好? – 2010-01-04 08:07:20

+0

@Jaco:如果你不想從舊的StringBuilder獲取任何信息,爲什麼要重用它?它增加了邏輯複雜性IMO,當你真的只想說,「我不需要舊的東西,給我一個新的。」 – 2010-01-04 08:20:16