0

Hopefully, this doesn't get marked as a duplicate. I've read a ton of different threads, but still not sure the best way to accomplish this.

Typically, I would just use global variables. From all the reading, it definitely appears that this shouldn't be done unless absolutely necessary. I'm sure that isn't the case for me. So I'm trying to follow best practice.

Let's just say I have something similar below.

public class myClass
{
    private void myMethod
    {

    }
}

How do I declare a static variable within myMethod? It seems I can only use static by delcaring it

private static myVariable 

But I cannot declare this from within my method.

If I declare this within myClass, then it is still of global scope and I believe it is limited to file scope. I could be wrong there. Is there not a way to limit the scope of the variable to myMethod?

Basically, I have a counter in that method that needs to retain its value throughout the duration of the program.

6
  • Looks like you want the behavior of static variable in C++. It's not possible to do in C#. Check this one What is the difference between a static variable in C++ vs. C#? Commented Apr 6, 2014 at 6:17
  • Global variables "should not be done"... hm... how do you do them if you want? They do not exist in C#. Commented Apr 6, 2014 at 6:25
  • @UlugbekUmirov Thanks for the link. Yeah, I was trying to use the static variable similarly to C. Looks like they are completely different. Thanks. Commented Apr 6, 2014 at 6:29
  • @TomTom What do you mean they don't exist? If I declared int myVariable right underneath myClass, wouldn't this be a global variable? Commented Apr 6, 2014 at 6:31
  • You're getting it wrong Commented Apr 6, 2014 at 6:31

2 Answers 2

3
public class myClass
{
    private static [your data type here] myVariable;
    private void myMethod
    {

    }
}

Your myMethod is looking like a property, so you can only do get; or set;.

The scope of static variables is only in the class not inside method.

Also, you are missing the data type. It can be any data type. For instance string.

Just remember that variable name should always be preceded by a data type.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 - You should make "The scope of static variables is only in the class not inside method" more visible in your answer as it is exactly what OP asked - there is no static values scoped to method in C# - see static
Member modifier static must precede the member type private static [your data type here] myVariable;
@FarhadJabiyev, yeah typo. Thnks
@AmitJoki I'm sure all of you can answer this question. Since you can only limit the scope of a variable to a class in c#, then what would be the difference in declaring the variable just a regular global variable instead of static? To prevent overwriting the static variable?
see, static variables don't need an object to be created. you can do myClass.myVariable if you declare it as public. If you want to prevent overwriteing search for "const in c#`"
0

why did you want your variable to be static if you only want it inside the the method.. just declare non static variable inside your method. There are two reasons C# doesn't have this feature.

First, it is possible to get nearly the same effect by having a class-level static, and adding method statics would require increased complexity.

Second, method level statics are somewhat notorious for causing problems when code is called repeatedly or from multiple threads, and since the definitions are in the methods, it's harder to find the definitions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.