是否可以在c#中更改同一类的不同函数内的静态字段值 [英] Is it possible to change the static field value inside different function of same class in c#

查看:225
本文介绍了是否可以在c#中更改同一类的不同函数内的静态字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在c#中更改同一类的不同函数内的静态字段值?任何人都可以帮助理解有用的例子。谢谢

解决方案

简单的答案是你可以设置Field的值,只要你有对该字段的有效引用。



如果你有一个公共静态字段,那么无论你在哪里在运行时有一个有效的引用到它定义的(NameSpace和/或)类名,你可以使用它。



如果您将非静态字段声明为public,则可以在任何您对其定义的实例具有有效引用的地方使用它。



当然,.NET中的所有访问都受声明字段/编写代码时使用的访问修饰符的约束。您将字段设置为只读,并且无法设置其值,依此类推。



您应该学习访问修饰符,包括:



public / private / internal / protected ...和readonly



如果你定义: public readonly static int Counter = 0;



你无法设置它,即使你有权访问它,但你当然可以读取它的值;


一个静态变量允许你可以通过在每次通过构造函数时向变量添加1来计算类的实例。

  class  Foo 
{
static int counter;
foo()
{
counter ++;
}

静态 void main()
{
Foo bar = new Foo();
Foo poo = new Foo();
Console.writeline(counter());
}
}





注意:您是否也发布了这个问题:http://www.codeproject.com/Messages/5167614/Using-static-var-on-a- class.aspx [ ^ ],在不同的用户ID下?


问题不明确,原因如下:更改同一类不同函数内的静态字段值主要意味着不同的东西。对这种内部情况的一种理解是:如果是A类,则存在静态字段。我可以在代码另一个函数中更改它吗?这个表述无效,因为没有相同或另一个功能。什么是不是另一个?字段只能在类中定义,而不能在函数中定义。如果你的意思是在函数内部定义了一些对象,那么它永远不是一个字段。所以,对这个问题的另一种理解就更有效了:我在一个函数中定义了一个静态字段,我可以在另一个函数中修改它吗? - 没有这样的东西。 />


因此,唯一的答案是:同一类的所有函数对此类的任何成员具有相同的访问权限。 静态没有区别。静态成员属于该类,而不属于任何特定实例。静态成员可以在类的静态成员和实例成员中访问,但是非静态成员(它们被称为实例成员都需要一些实例来操作。对于一个方法,它完成了传递实例作为一个名为this的隐式方法参数。静态方法没有它。



如果你想问我可以访问定义的局部变量吗?一个函数,从另一个函数的代码?(正如我已经说过,它不能是一个字段),答案是不。局部变量是在每个单独的堆栈框架中的堆栈上创建的,每次调用后都会丢弃一个堆栈帧。在返回之前,方法中声明的所有变量都只留在调用内部。



-SA

Is it possible to change the static field value inside different function of same class in c#??Can anyone help to make understand with useful example.Thanks

解决方案

The simple answer is that you can set the value of a Field wherever you have a valid reference to that field.

If you have a public static Field, then wherever you have a valid reference at run-time to the (NameSpace and/or) Class Name it is defined in, you can use it.

If you have a non-static Field declared 'public, you can use it anywhere you have a valid reference to the instance it is defined in.

Of course, all access in .NET is governed by the 'access modifiers used when declaring Fields / writing the code. You make a Field read-only, and you can't set its value, and so forth.

You should study the access modifiers including:

public/private/internal/protected ... and readonly

If you define: public readonly static int Counter = 0;

You can't set it, even if you have access to it, but you can, of course, read its value;


A static variable allows you to count instances of a class, by adding 1 to the variable each time you pass through the constructor.

class Foo
{
    static int counter;
    foo()
    {
        counter++;
    }

    static void main()
    {
        Foo bar = new Foo();
        Foo poo = new Foo();
        Console.writeline(counter());
   }
}



NB: Did you also post this question: http://www.codeproject.com/Messages/5167614/Using-static-var-on-a-class.aspx[^], under a different user id?


The question is not clear, by the following reason: "change the static field value inside different function of same class" main mean different things. One understanding of such "inside" situation would be: "There is a static field if a class A. Can I change it in the code another function?" This formulation would be invalid, because there is no "the same" or "another" function. What is "not another"? A field can be defined only in a class, not in a function. If you mean some object defined inside a function, it is never a field. So, the other understanding of this question would be even less valid: "I have a static field defined in one function, can I modify it in another function?" — there is no such thing.

So, the only answer would be: all functions of the same class have the same access to any member of this class. The "static" makes no difference. A static member belongs to the class and not to any particular instance. A static member can be accessed in both static and instance members of a class, but non-static members (they are called instance members all need some instance to operate. For a method, it is done be passing the instance as an implicit method parameters called "this". Static methods don't have it.

If you wanted to ask "can I access a local variable defined in one function, from the code of another function?" (as I already said, it cannot be a field), the answer would be "no". The local variables are created on the stack in each separate stack frame, and a stack frame is discarded after each call. All variables declared in a method leave only inside the call, before return.

—SA


这篇关于是否可以在c#中更改同一类的不同函数内的静态字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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