什么是C#中使用静态变量?当使用它?为什么我不能内声明的方法静态变量? [英] What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?

查看:523
本文介绍了什么是C#中使用静态变量?当使用它?为什么我不能内声明的方法静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜查约在C#中的静态变量,但我还是我没有得到它的用途是什么。另外,如果我试图声明在方法内部变量它不会给我这样做的权限。为什么呢?

我看到有关静态变量的一些例子。我已经看到了,我们并不需要创建类的实例,一个访问变量,但这是不够的,了解其使用,何时使用它。

第二件事

 类图书
{
    公共静态INT敏= 0;
}公共类运动
{
    静态无效的主要()
    {
        书书=新的图书();
        Console.WriteLine(book.myInt); //显示错误为什么它显示我的错误,我不能通过一个类的实例访问静态变量?
        Console.ReadKey();    }
}


解决方案

最后我明白静态变量.....静态变量共享类的所有实例中它的价值。

例未声明的静态

 公共类变量
{
    公众诠释我= 5;
    公共无效测试()
    {
        I = I + 5;
        Console.WriteLine(ⅰ);
    }
}
公共类运动
{
    静态无效的主要()
    {
        变量var =新变量();
        var.test();
        变量VAR1 =新变量();
        var1.test();
        Console.ReadKey();
    }
}

说明:如果你看一下上面的例子,我只是申报INT variable.when我运行这个code的输出将是10和10.Its简单

现在让我们在静态变量看这里,我把变量声明为静态。

使用静态变量示例

 公共类变量
{
    公共静态INT I = 5;
    公共无效测试()
    {
        I = I + 5;
        Console.WriteLine(ⅰ);
    }
}
公共类运动
{
    静态无效的主要()
    {
        变量var =新变量();
        var.test();
        变量VAR1 =新变量();
        var1.test();
        Console.ReadKey();
    }
}

现在,当我超过code运行,则输出将是类的所有实例之间共享10 15.so静态变量的值。

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 its shows me error?cant i access the static variable by making the instance of a class?
        Console.ReadKey();

    }
}

解决方案

Finally i understand the static variable.....static variable shared 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 int variable.when i run this code the output will be 10 and 10.Its simple

Now Lets 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 then the output will be 10 and 15.so static variable value shared among all instances of that class.

这篇关于什么是C#中使用静态变量?当使用它?为什么我不能内声明的方法静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆