静态修饰符如何在 C# 中工作
how does static modifier works in c#
我最近开始学习 C#,但我不太清楚的是 static
是什么意思以及它的行为方式。
例如,如果有一个 static
方法,我知道要使用该方法我需要调用 class
而不是对象的实例,但我真的不知道原因这给出了这种行为。
另一方面,对于 class
的特性,如果它们是 static
,我不太确定它们是如何修改的以及它们的行为方式
请注意,C# static 与 c++ 中的 static 不同。
来自Static Classes and Static Class Members (C# Programming Guide):
Static Member
The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it's explicitly passed in a method parameter.
及以后:
Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.
文中还有一个静态的例子属性:
public static int SizeOfGasTank
{
get
{
return 15;
}
}
我建议阅读全文 Static Classes and Static Class Members (C# Programming Guide)。
还有 Static Constructors (C# Programming Guide) 也值得一读,因为理解静态构造函数很重要,即使它们不经常使用也是如此。
请注意还有 using static 指令,这是完全不同的东西。
它们是如何工作的
如何 可能意味着很多事情,所以我不确定你到底在问什么,但是这一段再次来自 Static Classes and Static Class Members (C# Programming Guide) 提供了 [= =38=]静态方法在技术上是如何工作的?:
A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction, which also checks for null object references. However, most of the time the performance difference between the two is not significant.
我最近开始学习 C#,但我不太清楚的是 static
是什么意思以及它的行为方式。
例如,如果有一个 static
方法,我知道要使用该方法我需要调用 class
而不是对象的实例,但我真的不知道原因这给出了这种行为。
另一方面,对于 class
的特性,如果它们是 static
,我不太确定它们是如何修改的以及它们的行为方式
请注意,C# static 与 c++ 中的 static 不同。
来自Static Classes and Static Class Members (C# Programming Guide):
Static Member
The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it's explicitly passed in a method parameter.
及以后:
Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.
文中还有一个静态的例子属性:
public static int SizeOfGasTank { get { return 15; } }
我建议阅读全文 Static Classes and Static Class Members (C# Programming Guide)。
还有 Static Constructors (C# Programming Guide) 也值得一读,因为理解静态构造函数很重要,即使它们不经常使用也是如此。
请注意还有 using static 指令,这是完全不同的东西。
它们是如何工作的
如何 可能意味着很多事情,所以我不确定你到底在问什么,但是这一段再次来自 Static Classes and Static Class Members (C# Programming Guide) 提供了 [= =38=]静态方法在技术上是如何工作的?:
A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction, which also checks for null object references. However, most of the time the performance difference between the two is not significant.