Does C# support the use of static local variables?

后端 未结 12 928
眼角桃花
眼角桃花 2020-12-14 17:28

Related: How do I create a static local variable in Java?


Pardon if this is a duplicate; I was pretty sure this would have been

12条回答
  •  独厮守ぢ
    2020-12-14 17:53

    No, C# does not support this. You can come close with:

    private static System.Text.RegularExpressions.Regex re =
             new System.Text.RegularExpressions.Regex("\\(copy (\\d+)\\)$");
    
    private static string AppendCopyToFileName(string f)
    {
    
    }
    

    The only difference here is the visibility of 're'. It is exposed to the classm not just to the method.

    The re variable will be initialized the first time the containing class is used in some way. So keep this in a specialized small class.

提交回复
热议问题