小编Mic*_*ski的帖子

C#初始化局部变量一次

我认为我的问题将是完全愚蠢的,但我必须知道答案.

在这种情况下,是否可以初始化变量一次?

    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" + …
Run Code Online (Sandbox Code Playgroud)

c# initialization

5
推荐指数
1
解决办法
2736
查看次数

标签 统计

c# ×1

initialization ×1