7

辞書を宣言したかどうかはわかっているので、再利用するためにmyDict.Clear()を呼び出すことができます。

ここで、sbをStingBuilderobjとして宣言した場合。

StringBuilder sb = new StringBuilder();

sbを再利用する方法は?ありがとうございました。

実際には、mainDictのすべての可能な条件を印刷する必要があります。

このようなsb式の1つ(以下のコードに含まれています)

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

多くのStringBuilderobjを宣言した場合でも、十分なobjの数を検出できません。実際には、mainDictは非常に複雑です。上記のコードは練習のみです。ありがとう。


コードは1月4日に更新されました。

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
4

2 に答える 2

22

を0に設定できLengthます。.NET4.0にはClear()メソッドもあります。状態のドキュメントとしてClear

Clearは、現在のインスタンスのLengthプロパティを0(ゼロ)に設定するのと同等の便利なメソッドです。

だからそれは大したことではありません:)

あなたが本当に必要でない限り、私は個人的にこれを避けるでしょう-私は通常ただ新しいを作成しますStringBuilder。私の見解では、それは理解しやすいです-それはあなたが本当に前のオブジェクトから何ももう必要としないことをより明確にします。

オブジェクトを再利用したい特別な理由はありますか?パフォーマンス上の理由である場合、パフォーマンスを測定し、これがボトルネックであることがわかりましたか?非常に大容量のインスタンスがあり、それを再度割り当てないようにしたい場合は、重要になる可能性があると思います...しかし、それは私にはちょっとしたエッジケースのように感じます。

(これはすべて辞書にも当てはまります。ところで、最後に辞書をクリアしたときのことは思い出せません。)

于 2010-01-04T07:53:57.500 に答える
0

既存のStringBuilderオブジェクトを再利用するのではなく、新しいStringBuilderオブジェクトを作成する必要があると思います。

于 2010-01-04T07:58:15.120 に答える