私の質問は完全にばかげていると思いますが、答えを知らなければなりません。
この状況で変数を一度だけ初期化することは可能ですか?
static void Main()
{
while (true)
{
MethodA();
MethodB();
}
}
private static void MethodA()
{
string dots = string.Empty; // This should be done only once
if (dots != "...")
dots += ".";
else
dots = string.Empty;
Console.WriteLine(dots + "\t" + "Method A");
System.Threading.Thread.Sleep(500);
}
private static void MethodB()
{
string dots = string.Empty; // This should be done only once
if (dots != ".....")
dots += ". ";
else
dots = string.Empty;
Console.WriteLine(dots + "\t" + "Method B");
System.Threading.Thread.Sleep(500);
}
もちろん、メソッドから文字列ドットを初期化することはできますが、コードを混乱させたくはありません。これは、他のループ(forなど)でも実行できません。これをどのように解決するか、または私が普通に考えるのはとても愚かですか?
前もって感謝します。
編集:サンプルコードをより実用的なものに変更しました。必要な出力は次のとおりです。
. Method A
. Method B
.. Method A
.. Method B
... Method A
... Method B
Method A
.... Method B
. Method A
.....Method B
等