C#——关键字:static

C#——关键字:static

本页介绍 static 修饰符关键字。 static 关键字也是 using static 指令的一部分。

使用 static 修饰符可声明属于类型本身而不是属于特定对象的静态成员。 static 修饰符可用于声明 static 类。 在类、接口和结构中,可以将 static 修饰符添加到字段、方法、属性、运算符、事件和构造函数。 static 修饰符不能用于索引器或终结器。

从 C# 8.0 开始,可以将 static 修饰符添加到本地函数。 静态本地函数无法捕获局部变量或实例状态。

从 C# 9.0 开始,可将 static 修饰符添加到 Lambda 表达式或匿名方法。 静态Lambda 表达式或匿名方法无法捕获局部变量或实例状态。

示例 - 静态类

下面的类声明为 static 并且只含 static 方法:

static class CompanyEmployee
{
    public static void DoSomething() { /*...*/ }
    public static void DoSomethingElse() { /*...*/  }
}

常数或类型声明是隐式的 static 成员。 不能通过实例引用 static 成员。 然而,可以通过类型名称引用它。 例如,请考虑以下类:

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

若要引用 static 成员 x,除非可从相同范围访问该成员,否则请使用完全限定的名称 MyBaseC.MyStruct.x:

Console.WriteLine(MyBaseC.MyStruct.x);

尽管类的实例包含该类的所有实例字段的单独副本,但每个 static 字段只有一个副本。

不可以使用 this 引用 static 方法或属性访问器。

如果 static 关键字应用于类,则类的所有成员都必须为 static。

类、接口和 static 类可以具有 static 构造函数。 在程序开始和实例化类之间的某个时刻调用 static 构造函数。

备注
static 关键字比用于 C++ 中时受到的限制更多。 若要与 C++ 关键字进行比较,请参阅 Storage classes (C++)(存储类 (C++))。

若要演示 static 成员,请考虑表示公司员工的类。 假定此类包含计数员工的方法和存储员工人数的字段。 方法和字段均不属于任何一个员工实例。 相反,它们属于全体员工这个类。 应将其声明为该类的 static 成员。

示例 - 静态字段和方法

此示例读取新员工的姓名和 ID,员工计数器按 1 递增,并显示新员工信息和新员工人数。 此程序从键盘读取员工的当前人数。

public class Employee4
{
    public string id;
    public string name;

    public Employee4()
    {
    }

    public Employee4(string name, string id)
    {
        this.name = name;
        this.id = id;
    }

    public static int employeeCounter;

    public static int AddEmployee()
    {
        return ++employeeCounter;
    }
}

class MainClass : Employee4
{
    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.
        Employee4 e = new Employee4(name, id);
        Console.Write("Enter the current number of employees: ");
        string n = Console.ReadLine();
        Employee4.employeeCounter = Int32.Parse(n);
        Employee4.AddEmployee();

        // Display the new information.
        Console.WriteLine($"Name: {e.name}");
        Console.WriteLine($"ID:   {e.id}");
        Console.WriteLine($"New Number of Employees: {Employee4.employeeCounter}");
    }
}
/*
Input:
Matthias Berndt
AF643G
15
 *
Sample Output:
Enter the employee's name: Matthias Berndt
Enter the employee's ID: AF643G
Enter the current number of employees: 15
Name: Matthias Berndt
ID:   AF643G
New Number of Employees: 16
*/

示例 - 静态初始化

此示例演示了如何使用尚未声明的 static 字段来初始化另一个 static 字段。 在向 static 字段显式赋值之后才会定义结果。

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);
    }
}
/*
Output:
    0
    5
    99
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值