Static in C# .NET

来源:互联网 发布:linux命令vi的使用 编辑:程序博客网 时间:2024/06/04 18:40

使用 static 修饰符声明属于类型本身而不是属于特定对象的静态成员。static 修饰符可用于类、字段、方法、属性、运算符、事件和构造函数,但不能用于索引器、析构函数或类以外的类型。例如,下面的类声明为 static,并且只包含 static 方法。

        static class CompanyEmployee{    public static string GetCompanyName(string name) { ... }    public static string GetCompanyAddress(string address) { ... }}

有关更多信息,请参见静态类和静态类成员(C# 编程指南)。


 

  • 常数或者类型声明隐式地是静态成员。

  • 不能通过实例引用静态成员。然而,可以通过类型名称引用它。例如,请考虑以下类:

    public class MyBaseC {    public struct MyStruct     {        public static int x = 100;    }}

    若要引用静态成员 x,请使用完全限定名(除非可从相同范围访问):

    MyBaseC.MyStruct.x
  • 尽管类的实例包含该类所有实例字段的单独副本,但每个静态字段只有一个副本。

  • 不可以使用 this 来引用静态方法或属性访问器。

  • 如果对类应用 static 关键字,则该类的所有成员都必须是静态的。

  • 类(包括静态类)可以有静态构造函数。在程序开始和实例化类之间的某个时刻调用静态构造函数。

    Note注意

    static 关键字在使用上比在 C++ 中有更多的限制。若要与 C++ 关键字比较,请参见 static。

为了说明静态成员,请看一个表示公司雇员的类。假设该类包含一种对雇员计数的方法和一个存储雇员数的字段。该方法和字段都不属于任何实例雇员,而是属于公司类。因此,应该将它们声明为此类的静态成员。

示例

该示例读取新雇员的名称和 ID,逐个增加雇员计数器并显示新雇员的有关信息以及新的雇员数。为简单起见,该程序从键盘读取当前的雇员数。在实际的应用中,应从文件读取此信息。

// cs_static_keyword.csusing System;public class Employee{    public string id;    public string name;    public Employee()    {    }    public Employee(string name, string id)    {        this.name = name;        this.id = id;    }    public static int employeeCounter;    public static int AddEmployee()    {        return ++employeeCounter;    }}class MainClass : Employee{    static void Main()    {        Console.Write("Enter the employee's name: ");        string name = Console.ReadLine();        Console.Write("Enter the employee's ID: ");        string id = Console.ReadLine();        // Create and configure the employee object:        Employee e = new Employee(name, id);        Console.Write("Enter the current number of employees: ");        string n = Console.ReadLine();        Employee.employeeCounter = Int32.Parse(n);        Employee.AddEmployee();        // Display the new information:        Console.WriteLine("Name: {0}", e.name);        Console.WriteLine("ID:   {0}", e.id);        Console.WriteLine("New Number of Employees: {0}",                      Employee.employeeCounter);    }}

输入

Tara StrahanAF643G15

示例输出

Enter the employee's name: Tara StrahanEnter the employee's ID: AF643GEnter the current number of employees: 15Name: Tara StrahanID:   AF643GNew Number of Employees: 16

此示例显示:虽然可以用另一个尚未声明的静态字段实例化一个静态字段,但只要给静态字段显式赋值后,结果才是确定的。

// cs_static_keyword_2.csusing System;class Test{   static int x = y;   static int y = 5;   static void Main()   {      Console.WriteLine(Test.x);      Console.WriteLine(Test.y);      Test.x = 99;      Console.WriteLine(Test.x);   }}

输出

0599

 static的用法
要理解static,就必须要先理解另一个与之相对的关键字,很多人可能都还不知道有这个关键字,那就是auto,其实我们通常声明的不用static修饰的变量,都是auto的,因为它是默认的,就象short和long总是默认为int一样;我们通常声明一个变量:
int a;
string s;
其实就是:
auto int a;
auto string s;
而static变量的声明是:
static int a;
static string s;
这样似乎可以更有利于理解auto和static是一对成对的关键字吧,就像private,protected,public一样;
对于static的不理解,其实就是对于auto的不理解,因为它是更一般的;有的东西你天天在用,但未必就代表你真正了解它;auto的含义是由程序自动控制变量的生存周期,通常指的就是变量在进入其作用域的时候被分配,离开其作用域的时候被释放;而static就是不auto,变量在程序初始化时被分配,直到程序退出前才被释放;也就是static是按照程序的生命周期来分配释放变量的,而不是变量自己的生命周期;所以,像这样的例子:
void func()
{
int a;
static int b;
}
每一次调用该函数,变量a都是新的,因为它是在进入函数体的时候被分配,退出函数体的时候被释放,所以多个线程调用该函数,都会拥有各自独立的变量a,因为它总是要被重新分配的;而变量b不管你是否使用该函数,在程序初始化时就被分配的了,或者在第一次执行到它的声明的时候分配(不同的编译器可能不同),所以多个线程调用该函数的时候,总是访问同一个变量b,这也是在多线程编程中必须注意的!
static的全部用法:
1.类的静态成员:
class A
{
private:
static int s_;
};
在cpp中必须对它进行初始化:
int A::s_ = 0;// 注意,这里没有static的修饰!
类的静态成员是该类所有实例的共用成员,也就是在该类的范畴内是个全局变量,也可以理解为是一个名为A::s_的全局变量,只不过它是带有类安全属性的;道理很简单,因为它是在程序初始化的时候分配的,所以只分配一次,所以就是共用的;
类的静态成员必须初始化,道理也是一样的,因为它是在程序初始化的时候分配的,所以必须有初始化,类中只是声明,在cpp中才是初始化,你可以在初始化的代码上放个断点,在程序执行main的第一条语句之前就会先走到那;如果你的静态成员是个类,那么就会调用到它的构造函数;
2.类的静态函数:
class A
{
private:
static void func(int );
};
实现的时候也不需要static的修饰,因为static是声明性关键字;
类的静态函数是在该类的范畴内的全局函数,不能访问类的私有成员,只能访问类的静态成员,不需要类的实例即可调用;实际上,它就是增加了类的访问权限的全局函数:void A::func(int);
静态成员函数可以继承和覆盖,但无法是虚函数;
3.只在cpp内有效的全局变量:
在cpp文件的全局范围内声明:
static int g_ = 0;
这个变量的含义是在该cpp内有效,但是其他的cpp文件不能访问这个变量;如果有两个cpp文件声明了同名的全局静态变量,那么他们实际上是独立的两个变量;
如果不使用static声明全局变量:
int g_ = 0;
那么将无法保证这个变量不被别的cpp共享,也无法保证一定能被别的cpp共享,因为要让多个cpp共享一个全局变量,应将它声明为extern(外部)的;也有可能编译会报告变量被重复定义;总之不建议这样的写法,不明确这个全局变量的用法;
如果在一个头文件中声明:
static int g_vaule = 0;
那么会为每个包含该头文件的cpp都创建一个全局变量,但他们都是独立的;所以也不建议这样的写法,一样不明确需要怎样使用这个变量,因为只是创建了一组同名而不同作用域的变量;
这里顺便说一下如何声明所有cpp可共享的全局变量,在头文件里声明为extern的:
extern int g_; // 注意,不要初始化值!
然后在其中任何一个包含该头文件的cpp中初始化(一次)就好:
int g_ = 0; // 初始化一样不要extern修饰,因为extern也是声明性关键字;
然后所有包含该头文件的cpp文件都可以用g_这个名字访问相同的一个变量;
4.只在cpp内有效的全局函数:
在cpp内声明:
static void func();
函数的实现不需要static修饰,那么这个函数只可在本cpp内使用,不会同其他cpp中的同名函数引起冲突;道理和如果不使用static会引起的问题和第3点一样;不要在头文件中声明static的全局函数,不要在cpp内声明非static的全局函数,如果你要在多个cpp中复用该函数,
就把它的声明提到头文件里去,否则在cpp内部声明需要加上static修饰;在C语言中这点由为重要!
Static:当一个方法被声明为Static时,这个方法是一个静态方法,编译器会在编译时保留这个方法的实现。也就是说,这个方法属于类,但是不属于任何成员,不管这个类的实例是否存在,它们都会存在。就像入口函数Static void Main,因为它是静态函数,所以可以直接被调用

STATIC in C# .NET

By Peter A. Bromberg, Ph.D.

 

One of the biggest problems we VB developers migrating to C# face is understanding STATIC. Static constructors, static data, static member functions, static properties.

To a VB developer, "static" means a variable inside a function (method) whose value remains "live" between function calls, but which is invisible outside the function. In C#, there is no such concept, because it's not necessary when you can define member fields instead.

 

Let's run through the concept of STATIC and explore its major uses.

In C#, whenever you declare a variable which is bound to an instance (object) of a class, your variable by default gets its own copy of all the fields in the class. So for example, if you write:

CreditCardAccountPremium cp1 = new CreditCardAccountPremium();
CreditCardAccountPremium cp2 = new CreditCardAccountPremium();
cp1.setFirstName("George");
cp2.setFirstName("Harry");

-- the instances cp1 and cp2 each contain their own string called FirstName. Changing the FirstName in cp2 has no effect on the value of the FirstName in cp1. However, there are some cases where you do not want this behavior. Maybe you would like to set a maximum value for a CashAdvance that is common to the entire CreditCardAccountPremium class. This would be universally applied to all instances of CreditCardAccountPremium.

We really want the "maxCashAdvance" to be stored in memory only once, no matter how many instances of the class we instantiate. To do this, we place the keyword static before the field declaration:

public class CreditCardAccountPremium
{
      private static uint maxCashAdvance = 300;
      private string FirstName;
      private string LastName;
      ....

Fields declared with the static keyword are referred to as static fields or static data., while the other fields like FirstName are called instance fields. So an instance field belongs to an object, and a static field belongs to the class as a whole. Static fields exist from the moment the assembly containing the class is loaded, and are initialized by the runtime, independent of whether you actually declare any instances of the class. Consequently, we can also have static constructors, which serve no other purpose than to assign initial values to static data:

public class CreditCardAccountSuper
{
     private static uint maxCashAdvance;
    staticCreditCardAccountSuper()
       {
           maxCashAdvance = 300;
       }

If you were to invoke a CreditCardAccountSuper.maxCashAdvance property, there is no need to assign an initial value within the Main() method; the static constructor does this automatically. Note that the static constructor in which the maxCashAdvance default value is set has the same name as the class, and cannot take any arguments.

Just as is the case with fields, you can declare methods as static provided they don't try to access instance data or any other instance methods. You might want to provide a method to allow users of the class to view the maxCashAdvance value:

public class CreditCardAccountPremium
{
      private static uint maxCashAdvance = 300;
      private string FirstName;
      private string LastName;

public static uint GetMaxCashAdvance()
     {
       return maxCashAdvance;
     }  

You access static methods and fields associated with a class differently that you access them as objects (instances of a class). Instead of specifying the name of the instance to access the method (cp1.GetMaxCashAdvance) you will specify the name of the class: CreditCardAccountPremium.GetMaxCashAdvance(). You can also access the static members of a class with the "this" operator: this.maxCashAdvance. The "this" operator is analogous to the VB "Me" keyword, and is used whenever you want to make reference to the current object instance. The static modifier can be used with fields, methods, properties, operators, and constructors, but cannot be used with indexers, destructors, or types.   C# trashes the whole concept of global functions, variables, and constants. Instead, you can create static class members, making your C# code not only easier to read, but less likely to cause naming and other conflicts.

I hope this short discussion helps things tune in better, because the static is all there for a good reason!

 


 public   static   string   Abc(){   
  return(@"abcdefg");  
  }  
   
  public   string   Efg(){  
  return(@"Efghdks");  
  }  
   

  以上两个function   有什么区别?     运行速度,或内存占用有区别么?
 0、1.静态成员,访问不需要实例化     2.非表态成员,访问需要实例化
1、you   should   read   a   basic   C#   programming   book   
   
  to   call   Abc(),   you   do    
  string   s   =   YourClassName.Abc();  
   
  to   call   Efd(),   you   need   to   create   an   instance   first  
   
  YourClassName   ycn   =   new   YourClassName();  
  string   s   =   ycn.Efd();  
   

  but   in   Efd(),   you   can   use   instance   memeber   variables   directly,   in   Abc(),   you   can   only   use   static   variables   directly  
2、一个是类调用,一个是对象调用。
Static   函数在使用时不用实例化它所在的类,直接用就得了。就象Convert.ToInt32()等这些函数。   
  不用Static的函数,就必须实例化它所在的类进行使用。
3、static   ,直接里调用,是属于该类的;否则,是属于该实例话的对象的;
4、从内存上来说,   用   new   实例化的时候,就创建了所有静态空间的内存空地址. 
除了内存的区别外,  还是代码维护的问题了。     当参数个数改变了,实例方法修改起来比较方便。
5、性能上我认为差别不大,或者说感觉不到的。但是我认为调用非静态成员是   
  通过new一个实例,当不再用这些成员时实例自然被dispose,这样就实现内存回收,而使用静态成员是直接使用类名来访问的,它只有在退出系统的时候可能才会实现内存回收吧。  
  这只是我个人的看法,也许理解有误。最大限度地提高代码的可重用性,改写类的实例方法,把参数类型改成接口,选择最简单的参数接口类型

 
原创粉丝点击