1

I am new to C# programming and I am currently using many static variables in my code. Below is one example:

class Program
{
    public int I//Can be used to access the variable i anywhere from the code as it is public
    {
        get { return i; }
        set { i = value; }
    }

    static int i = 0; // A private static integer
    static void Main(string[] args)
    {
        i = 1;// The main function is changing this integer
    }

    void reset() { 
        i = 0;// another function is changing the value of integer i
    }
}

class otherclass 
{
    void otherreset()
    {
        Program program = new Program();
        program.I = 1;// another function in another class is changing the value of integer i(indirectly)   

    }

}
  1. The static variable i is available to all the functions in that class.
  2. Then there is I which shares i with every function in the code as it is public. - not sure if I should even be doing this.

I did find this thread about using static variables in C# but I'd like to know, if this is a standard practice from a security perspective. I am concerned that the variable resides in the same location in memory throughout the execution of the program.

Are there any other better ways in general to share a variable between various functions.

Community
  • 1
  • 1
JOW
  • 244
  • 4
  • 18
  • Can you explain how this relates to "security"? What exactly do you want to "secure"? – CodeCaster Feb 10 '16 at 16:42
  • 3
    `are there any other better ways in general to share a variable between various functions.` Yes, there are lots of other ways. In virtually all cases you should be using a method's parameters to pass it data, not setting a static field. – Servy Feb 10 '16 at 16:43
  • @CodeCaster I want to know if keeping the value contained in the variable available in memory for a longer time than usual a concern. – JOW Feb 10 '16 at 16:51
  • The way you're setting that static field in the `otherreset` method doesn't really make sense either. You're creating an object, then using the instance of that object to modify the static variable. Skip the middle man by making `i` public, then you can modify it by writing `Program.i = value`. – Mage Xy Feb 10 '16 at 16:51
  • @MageXy at certain situations, VS compains that I have to use an object reference for i even if I make it Public. – JOW Feb 10 '16 at 16:54
  • @Servy so even if I am forced to use a void function and I pass a variable as a parameter, then I assume, I should use "out" to get the changed value. – JOW Feb 10 '16 at 17:11
  • @JOW Why are you forced to use a void function? – Servy Feb 10 '16 at 17:22
  • @Servy sometimes I end up with an async void method when i'm working with asynchronous event handlers. – JOW Feb 10 '16 at 17:32
  • 1
    @JOW Then there is no need to return a value from such a method, because it's an event handler. The type invoking the event isn't going to be expecting to get a value out of the method. – Servy Feb 10 '16 at 17:33
  • @Servy thanks. seems like i have to avoid async void methods too. http://haacked.com/archive/2014/11/11/async-void-methods/ – JOW Feb 10 '16 at 17:45

1 Answers1

2
static int i = 0; // A private static integer

Here you declared a static member. So far so good.

static void Main(string[] args)
{
    int i = 1;// The main function is changing this integer
}

No, it doesn't. int i means you declared a new local variable named i.

void reset() { 
    int i = 0;// another function is changing the value of integer i
}

No, it doesn't. int i means you declared a new local variable named i.

I'd like to know if this is a standard practice from a security perspective.

No, it isn't, and no, it's not related to security. You should avoid using static members as much as possible. I think that the actual reasons why are, currently, way too far from your current level of knowledge and understanding. Just take it as a best practice.

I am concerned that the variable resides in the same location in memory throughout the execution of the program.

Well, a variable that would 'travel' between memory locations wouldn't be a good idea :-) Variables are, very simplistically put, named locations in memory.

are there any other better ways in general to share a variable between various functions.

Yes, start by learning about function parameters, member fields, and properties. Use whatever beginner C# book you have available, or search online.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90