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)
}
}
- The static variable i is available to all the functions in that class.
- 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.