C# 静态关键字
C# 静态关键字
在本教程中,我们将通过示例了解 C# 中的 static 关键字。
在 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 的静态变量 .
程序内部 类,
s1.studentName
/s2.studentName
- 使用对象 s1 调用非静态变量 和 s2 分别Student.schoolName
- 使用类名调用静态变量
自 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
在上面的程序中,我们声明了一个名为 display1() 的非静态方法 和一个名为 display2() 的静态方法 在类 Test 中 .
在 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 .我们创建了一个对象 t1 Test 类的 .
由于我们无法创建静态类的对象,因此会出现以下错误:
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()
不使用类名。
C语言