What is the use of static variable in C#? When to

2019-01-01 13:10发布

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();
    }
}

12条回答
笑指拈花
2楼-- · 2019-01-01 13:27

A static variable shares the value of it among all instances of the class.

Example without declaring it static:

public class Variable
{
    public int i = 5;
    public void test()
    {
        i = i + 5;
        Console.WriteLine(i);
    }
}


public class Exercise
{
    static void Main()
    {
        Variable var = new Variable();
        var.test();
        Variable var1 = new Variable();
        var1.test();
        Console.ReadKey();
    }
}

Explanation: If you look at the above example, I just declare the int variable. When I run this code the output will be 10 and 10. Its simple.

Now let's look at the static variable here; I am declaring the variable as a static.

Example with static variable:

public class Variable
{
    public static int i = 5;
    public void test()
    {
        i = i + 5;
        Console.WriteLine(i);
    }
}


public class Exercise
{
    static void Main()
    {
        Variable var = new Variable();
        var.test();
        Variable var1 = new Variable();
        var1.test();
        Console.ReadKey();
    }
}

Now when I run above code, the output will be 10 and 15. So the static variable value is shared among all instances of that class.

查看更多
像晚风撩人
3楼-- · 2019-01-01 13:28

Try calling it directly with class name Book.myInt

查看更多
呛了眼睛熬了心
4楼-- · 2019-01-01 13:29

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.

查看更多
几人难应
5楼-- · 2019-01-01 13:30

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().

查看更多
荒废的爱情
6楼-- · 2019-01-01 13:36

You don't need to instantiate an object, because yau are going to use a static variable: Console.WriteLine(Book.myInt);

查看更多
永恒的永恒
7楼-- · 2019-01-01 13:37

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

class myclass
{
    public static int a = 0;
}

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.

查看更多
登录 后发表回答