在 C# 中,如果我们将 `static` 关键字用于类成员,则该类型成员将只有一个副本。
并且,类的所有对象共享一个副本,而不是创建单独的副本。
C# 静态变量
如果一个变量被声明为 `static`,我们可以使用类名来访问该变量。例如:
using System;
namespace StaticKeyword {
class Student {
// static variable
public static string department = "Computer Science";
}
class Program {
static void Main(string[] argos) {
// access static variable
Console.WriteLine("Department: " + Student.department);
Console.ReadLine();
}
}
}
输出
Department: Computer Science
在上面的示例中,我们创建了一个名为 `department` 的静态变量。由于该变量是静态的,我们使用 `Student` 类名来访问该变量。
静态变量与实例变量
在 C# 中,类的每个对象都有自己的实例变量副本。例如:
class Student {
// instance variable
public string studentName;
}
class Program {
static void Main(string[] args) {
Student s1 = new Student();
Student s2 = new Student();
}
}
在这里,对象 `s1` 和 `s2` 都将拥有 `studentName` 变量的单独副本。而且,它们彼此不同。
然而,如果我们声明一个变量为静态,类的所有对象将共享同一个静态变量。而且,我们不需要创建类的对象来访问静态变量。
示例:C# 静态变量与实例变量
using System;
namespace StaticKeyword {
class Student {
static public string schoolName = "Programiz School";
public string studentName;
}
class Program {
static void Main(string[] args) {
Student s1 = new Student();
s1.studentName = "Ram";
// calls instance variable
Console.WriteLine("Name: " + s1.studentName);
// calls static variable
Console.WriteLine("School: " + Student.schoolName);
Student s2 = new Student();
s2.studentName = "Shyam";
// calls instance variable
Console.WriteLine("Name: " + s2.studentName);
// calls static variable
Console.WriteLine("School: " + Student.schoolName);
Console.ReadLine();
}
}
}
输出
Name: Ram School: Programiz School Name: Shyam School: Programiz School
在上面的程序中,`Student` 类有一个名为 `studentName` 的非静态变量和一个名为 `schoolName` 的静态变量。
在 `Program` 类中:
s1.studentName
/s2.studentName
- 分别使用对象 `s1` 和 `s2` 调用非静态变量Student.schoolName
- 使用类名调用静态变量
由于 `schoolName` 对所有学生都相同,因此将其设为静态是很好的。这可以节省内存并使程序更高效。
C# 静态方法
与静态变量一样,我们可以使用类名来调用静态方法。
class Test {
public static void display() {....}
}
class Program {
static void Main(string[] args) {
Test.display();
}
}
在这里,我们直接从 `Program` 类使用类名访问了静态方法。
当我们声明一个方法为静态时,类的所有对象将共享同一个静态方法。
示例:C# 静态和非静态方法
using System;
namespace StaticKeyword {
class Test {
public void display1() {
Console.WriteLine("Non static method");
}
public static void display2() {
Console.WriteLine("Static method");
}
}
class Program {
static void Main(string[] args) {
Test t1 = new Test();
t1.display1();
Test.display2();
Console.ReadLine();
}
}
}
输出
Non static method Static method
在上面的程序中,我们在 `Test` 类中声明了一个名为 `display1()` 的非静态方法和一个名为 `display2()` 的静态方法。
在 Program 类中:
t1.display1()
- 使用 `s1` 对象访问非静态方法Test.display2()
- 使用类名 `Test` 访问静态方法
注意:在 C# 中,`Main` 方法是静态的。因此,我们可以不创建对象就调用它。
C# 静态类
在 C# 中,当我们声明一个类为静态时,我们不能创建该类的对象。例如:
using System;
namespace StaticKeyword {
static class Test {
static int a = 5;
static void display() {
Console.WriteLine("Static method");
}
static void Main(string[] args) {
// creating object of Test
Test t1 = new Test();
Console.WriteLine(a);
display();
}
}
}
在上面的示例中,我们有一个名为 `Test` 的静态类。我们创建了 `Test` 类的一个对象 `t1`。
由于我们无法创建静态类的对象,因此我们会收到以下错误:
error CS0723: Cannot declare a variable of static type 'Test'
error CS0712: Cannot create an instance of the static class
请注意,静态类的字段和方法也都是静态的,因为静态类中只能包含静态成员。
注意:我们不能在 C# 中继承静态类。例如:
static class A {
...
}
// Error Code
class B : A {
...
}
在类内访问静态成员
如果我们正在访问同一类内的静态变量和方法,我们可以直接访问它们,而无需使用类名。例如:
using System;
namespace StaticKeyword {
class Test {
static int age = 25;
public static void display() {
Console.WriteLine("Static method");
}
static void Main(string[] args) {
Console.WriteLine(age);
display();
Console.ReadLine();
}
}
}
输出
25 Static method
在这里,我们正在访问静态字段 `age` 和静态方法 `display()`,而无需使用类名。