c#静态函数

仅仅一个静态函数能被定义在一个类中; 我们声明一个静态构造函数通过在它的名字
前加上static关键字。在缺省的情况下,静态构造函数是public,如果我们强制的加上
public关键字 将导致一个错误。静态构造函数的参数序列必须为空。
静态构造函数有且只能被调用一次
我们能够初始化静态成员或者调用静态成员在静态构造函数中

using System;

namespace 静态构造函数
{
 
 class xiaobiaofamily
    {
  private static int age;
  private string name;
  private static string bloodtype;
  private int num;

  public xiaobiaofamily()
  {
           hanshu();
   // name ="xiaobiao"; 这里报错  因为name 是实例变量 不是静态变量  而静态构造函数只能初始化静态变量
     }
  public xiaobiaofamily( string name, int num)
  {
   age = 99;
   this.name =name;
   this.num = num;

  }
  
  public void hanshu()
  {
           age = 8;
     bloodtype = "o";
     name = "xiaobiao";
     num = 33;
  }
 
  static void Main(string[] args)
  {
   xiaobiaofamily xiaobiao1 = new xiaobiaofamily("xiaobiao",88);
            xiaobiaofamily xiaobiao2 = new xiaobiaofamily();

   Console.WriteLine("xiaobiao1:age:{0},name:{1},num:{2},bloodtype:{3}",age,xiaobiao1.name,xiaobiao1.num,bloodtype);
   Console.WriteLine("xiaobiao2:age:{0},name:{1},num:{2},bloodtype:{3}",age,xiaobiao2.name,xiaobiao2.num,bloodtype);
   
   Console.ReadLine();
  }
 }
}


我们能用它(静态构造函数)去执行一些行为在我们初始化类对象或则访问类的静态变量之前。
静态构造函数可以初始化静态成员变量 但一定不能初始化非静态的成员变量
但非静态构造函数中可以初始化静态变量 比如下面的例子
静态构造函数在非静态方法之前被执行
就以下面的例子说明
在Main()方法中
程序开始执行
1先调用静态构造函数 初始化了 静态成员变量
2然后看到new关键字
  new关键字的作用的计算需要分配的内存大小和分配内存
  如果分配成功就在调用实例构造函数 初始化非静态成员变量(实例变量)
  如果内存没有所需的大小 则调用GC 进行垃圾回收 腾出一部分空间来。
  GC的作用就是管理内存的,不象C++需要程序员自己处理内存上的垃圾回收)
 把大致意思说了一下  如果你看了我给你的那本书 就很容易理解,东西很多
我一下子讲不清楚,自己抽空看看拉。

 

using System;

namespace 静态构造函数
{
 
 class xiaobiaofamily
 {
  private static int age;
  private string name;
  private static string bloodtype;
  private int num;

  static xiaobiaofamily()
  {
   age = 10;
   bloodtype ="o";
   // name ="xiaobiao"; 这里报错  因为name 是实例变量 不是静态变量  而静态构造函数只能初始化静态变量
  }
  public xiaobiaofamily( string name, int num)
  {
   //age = 99;
   this.name =name;
   this.num = num;

  }
  
 
  static void Main(string[] args)
  {
   xiaobiaofamily xiaobiao1 = new xiaobiaofamily("xiaobiao",88);
   xiaobiaofamily xiaobiao2 = new xiaobiaofamily("xiaobiao",883);

   Console.WriteLine("xiaobiao1:age:{0},name:{1},num:{2},bloodtype:{3}",age,xiaobiao1.name,xiaobiao1.num,bloodtype);
   Console.WriteLine("xiaobiao2:age:{0},name:{1},num:{2},bloodtype:{3}",age,xiaobiao2.name,xiaobiao2.num,bloodtype);
   
   Console.ReadLine();
  }
 }
}

 

注意5点(静态构造函数)
1 A given class (or structure) may define only a single static constructor.
2 A static constructor executes exactly one time, regardless of how many objects of the type are
created.
3 A static constructor does not take an access modifier and cannot take any parameters.
4 The runtime invokes the static constructor when it creates an instance of the class or before
accessing the first static member invoked by the caller.
5 The static constructor executes before any instance-level constructors.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值