In: Computer Science
How does one declare a class variable in C#?
Class Variable:
In some languages, static member variable or static member function are used synonymously with or in place of "class variable" or "class function".The Class variable is also Known as Static variable.
Static variables have a property of preserving their value even after they are out of their scope!Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.
Example:
Declarartion of static variables(Class Variable) in C:
In C, static variables can only be initialized using constant literals. For example, following program fails in compilation.
#include<stdio.h>
int initializer(void)
{
return 50;
}
int main()
{
static int i = initializer();
printf(" value of i = %d", i);
getchar();
return 0;
}
If we change the program to following, then it works without any error.
#include<stdio.h>
int main()
{
static int i = 50;
printf(" value of i = %d", i);
getchar();
return 0;
}
This is the correct way to Declare a Class Variable(static varible) in C.