简体   繁体   中英

C# Initialize local variable once

I assume my question will be totally stupid but I have to know the answer.

Is it possible to initialize a variable just once in this situation?

    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);
    }

Of course I can initialize string dots out of method but I don't want to do mess in the code, and this can't be done in any other loop too (like for). Any ideas how solve this or am I so stupid to think normally?

Thanks in advance.

EDIT: I've changed the sample code to be more practical. Desired output should be:

.    Method A
.    Method B
..   Method A
..   Method B
...  Method A
...  Method B
     Method A
.... Method B
.    Method A
.....Method B

Etc. etc.

You could keep dots in your class, and initialize it when the class is created, ie

string dots = string.Empty; 
private void Method()
{
    if (dots != "...")
        dots += ".";
    else
        dots = string.Empty;
}

You said you don't want to keep the dots out side of Method (in the Method's class), then you must return the value from Method so that you can at least pass it in later on, thus persisting its state.

string Method(string dot = string.Empty)
{
   if(dot == "...") return string.Empty;
   else return dot + ".";
}

var result = Method(Method(Method(Method(Method(Method()))))) // etc...

EDIT: Your edited question does not make your initial problem more practical. It still suffers from the same problem: You want X but C# does not have X. Use C++ or VB.NET instead.

The answer to your question

"Is it possible to initialize a variable just once in this situation?"

is Sorry, NO!

In other languages (C++) you can declare static variables at any scope. This would solve your issue neatly but C# doesn't permit declaration of static variables within functions. This is actually a C# FAQ: Why doesn't C# support static method variables? .

This design feature means that you cannot do what you want in a conventional method. You might be able to do something particularly cunning with currying, but if you don't want to mess with the existing program structure, that's out. I'm sure that there's a way to write native code that would get you to what you want, but it feels like a bad idea.

Putting it simply, you're asking for data that persists outside of its scope. In C#, your chief (sole?) remedy is to increase that data's scope.

You are looking for persistent data between calls to the method, so you need a data element outside of your call. You don't have static local variables in C#.

Consider reading this Stackoverflow post.

Are you thinking of a static local variable as in C++ ? They are not supported in C#, as discussed here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM