解释静态属性 [英] Explain static properties

查看:89
本文介绍了解释静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释静态属性和静态属性的缺点

can anybody explain static properties and disadvantage of static properties

推荐答案

MSDN [ ^ ]::)



非静态类可以包含静态方法,字段,属性或事件。即使没有创建类的实例,静态成员也可以在类上调用。静态成员始终由类名访问,而不是实例名。无论创建了多少个类实例,都只存在一个静态成员的副本。静态方法和属性无法访问其包含类型中的非静态字段和事件,并且除非在方法参数中显式传递,否则它们无法访问任何对象的实例变量。



使用一些静态成员声明非静态类比将整个类声明为静态更典型。静态字段的两个常见用途是保持已实例化的对象数量的计数,或存储必须在所有实例之间共享的值。



静态方法可以重载但不会被覆盖,因为它们属于类,而不属于类的任何实例。

虽然字段不能声明为静态const,但const字段本质上是静态的它的行为。它属于类型,而不属于该类型的实例。因此,可以使用与静态字段相同的ClassName.MemberName表示法来访问const字段。不需要对象实例。



C#不支持静态局部变量(在方法范围内声明的变量)。

你声明静态类成员通过在成员的返回类型之前使用static关键字,如以下示例所示:

Ripped from MSDN[^] : :)

A non-static class can contain static methods, fields, properties, or events. 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 is explicitly passed in a method parameter.

It is more typical to declare a non-static class with some static members, than to declare an entire class as static. 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.

Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.
Although a field cannot be declared as static const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type. Therefore, const fields can be accessed by using the same ClassName.MemberName notation that is used for static fields. No object instance is required.

C# does not support static local variables (variables that are declared in method scope).
You declare static class members by using the static keyword before the return type of the member, as shown in the following example:
public class Automobile
{
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
    {
        get
        {
            return 15;
        }
    }
    public static void Drive() { }
    public static event EventType RunOutOfGas;

    // Other non-static fields and properties...
}



静态成员在第一次访问静态成员之前和静态构造函数(如果有的话)被调用之前被初始化。要访问静态类成员,请使用类的名称而不是变量名来指定成员的位置,如以下示例所示:


Static members are initialized before the static member is accessed for the first time and before the static constructor, if there is one, is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member, as shown in the following example:

Automobile.Drive();
int i = Automobile.NumberOfWheels;



如果您的类包含静态字段,请提供静态加载类时初始化它们的构造函数。



调用静态方法会生成Microsoft中间语言(MSIL)的调用指令,而对实例的调用method生成一个callvirt指令,该指令还检查空对象引用。但是,大多数时候两者之间的性能差异并不显着。



C#静态属性 [ ^ ]。



-KR


If your class contains static fields, provide a static constructor that initializes them when the class is loaded.

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 a null object references. However, most of the time the performance difference between the two is not significant.

C# Static Property[^].

-KR


MSDN写道:

属性可以标记为public,private,protected,internal或protected internal。这些访问修饰符定义类的用户如何访问该属性。同一属性的get和set访问器可能具有不同的访问修饰符。例如,get可以是公共的,允许从类型外部进行只读访问,并且该集可以是私有的或受保护的。



属性可以声明为使用 static 关键字获取静态属性。 这使得该属性可以随时用于调用者,即使该类的实例不存在。有关更多信息,请参阅静态类和静态类成员(C#编程指南) [ ^ ]。

Properties can be marked as public, private, protected, internal, or protected internal. These access modifiers define how users of the class can access the property. The get and set accessors for the same property may have different access modifiers. For example, the get may be public to allow read-only access from outside the type, and the set may be private or protected.

A property may be declared as a static property by using the static keyword. This makes the property available to callers at any time, even if no instance of the class exists. For more information, see Static Classes and Static Class Members (C# Programming Guide)[^].



source:使用属性(C#编程指南) [ ^ ]


这篇关于解释静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆