I have searched about static variables in C#, but I still am not getting what its use is. Also, if I try to declare the variable inside the method it will not give me the permission to do this. Why?
I have seen some examples about the static variable. I've seen that we don't need to create a instance of the class to access the variable, but that is not enough to understand what its use is and when to use it.
Second thing
class Book
{
public static int myInt = 0;
}
public class Exercise
{
static void Main()
{
Book book = new Book();
Console.WriteLine(book.myInt); // Shows error why does it show me error?
// Can't I access the static variable
// by making the instance of a class?
Console.ReadKey();
}
}
A
static
variable shares the value of it among all instances of the class.Example without declaring it static:
Explanation: If you look at the above example, I just declare the
int
variable. When I run this code the output will be10
and10
. Its simple.Now let's look at the static variable here; I am declaring the variable as a
static
.Example with static variable:
Now when I run above code, the output will be
10
and15
. So the static variable value is shared among all instances of that class.Try calling it directly with class name
Book.myInt
The data members and function members that operate on the instance of the type are called instance members. The int’s ToString method (for example) are examples of instance members. By default, members are instance members. Data members and function members that don’t operate on the instance of the type, but rather on the type itself, must be marked as static. The Test.Main and Console.WriteLine methods are static methods. The Console class is actually a static class, which means all its members are static. You never actually create instances of a Console—one console is shared across the whole application.
Static variable retains it's previous value until the program exit. Static is used by calling directly class_Name.Method() or class_Name.Property. No object reference is needed. The most popular use of static is C#'s Math class. Math.Sin(), Math.Cos(), Math.Sqrt().
You don't need to instantiate an object, because yau are going to use a static variable: Console.WriteLine(Book.myInt);
static variables are used when only one copy of the variable is required. so if you declare variable inside the method there is no use of such variable it's become local to function only..
example of static is
Variables declared static are commonly shared across all instances of a class.
Variables declared static are commonly shared across all instances of a class. When you create multiple instances of VariableTest class This variable permanent is shared across all of them. Thus, at any given point of time, there will be only one string value contained in the permanent variable.
Since there is only one copy of the variable available for all instances, the code this.permament will result in compilation errors because it can be recalled that this.variablename refers to the instance variable name. Thus, static variables are to be accessed directly, as indicated in the code.