简体   繁体   English

C#初始化局部变量一次

[英]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). 当然我可以从方法中初始化字符串点,但我不想在代码中弄乱,而且这也不能在任何其他循环中完成(比如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 您可以在类中保留dots ,并在创建类时初始化它,即

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. 你说你不想把方法的点放在方法的一边(在方法的类中),然后你必须从Method返回值,这样你至少可以在以后传递它,从而保持其状态。

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. 它仍然遇到同样的问题:你想要X但C#没有X.请改用C ++或VB.NET。

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. 在其他语言(C ++)中,您可以在任何范围内声明静态变量。 This would solve your issue neatly but C# doesn't permit declaration of static variables within functions. 这样可以很好地解决您的问题,但C#不允许在函数内声明静态变量。 This is actually a C# FAQ: Why doesn't C# support static method variables? 这实际上是一个C#FAQ: 为什么C#不支持静态方法变量? .

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. 在C#中,您的主要(唯一?)补救措施是增加该数据的范围。

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#. 您在C#中没有静态局部变量。

Consider reading this Stackoverflow post. 考虑阅读此Stackoverflow帖子。

Are you thinking of a static local variable as in C++ ? 您是否正在考虑使用C ++中的静态局部变量 They are not supported in C#, as discussed here . 他们不是在C#的支持,为讨论在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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